我们都知道要转换字符串中的值,我们可以按照
进行操作char* buffer = ... allocate a buffer ...
int value = 4564;
sprintf(buffer, "%d", value);
但是如果我想将数据转换为整数缓冲区而不是字符缓冲区,我们该怎么办呢,基本上我想做以下
int* buffer = ... allocate a buffer ...
int value = 4564;
sprintf(buffer, "%d", value);
提前致谢
答案 0 :(得分:0)
确保将缓冲区定义为“value”的值,而不是指向“value”地址的指针。见下文:
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
int main(int argc, char** argv)
{
/* Allocate memory. */
int* buffer = malloc(sizeof(int));
int value = 1000;
/* Sets the number stored in buffer equal to value. */
*buffer = value;
/* prints 1000 */
printf("%d\n", *buffer);
/* Change value to show buffer is not pointing to the address of 'value'. */
value = 500;
/* Still prints 1000. If we had used
int* buffer = &value, it would print 500. */
printf("%d\n", *buffer);
return 0;
}