简体   繁体   English

将int格式存储为char * [C]

[英]Store an int in a char* [C]

int main()
{
   int n = 56;
   char buf[10]; //want char* buf instead
   sprintf(buf, "%i", n);
   printf("%s\n", buf);

   return 0;
}

This piece of code works, my question is what if i want the same behaviour, with buf being a char* ? 这段代码有效,我的问题是,如果我想要相同的行为,而buf是char *,该怎么办?

The main difference when writing 写作时的主要区别

char* buf;

is that it is uninitialized and no memory is allocated, so you'll have to take care yourself: 它是未初始化的,并且没有分配内存,因此您必须保重:

char* buf = malloc(10 * sizeof(char));
// ... more code here
free(buf); // don't forget this or you'll get a memory leak

This is called dynamic memory allocation (as opposed to static allocation) and it allows you nice things like changing the amount of allocated memory at runtime using realloc or using a variable in the malloc call. 这称为动态内存分配 (相对于静态分配),它允许您进行一些不错的操作,例如在运行时使用realloc或在malloc调用中使用变量来更改分配的内存量 Also note that memory allocation can fail if the amount of memory is too large, in this case the returned pointer will be NULL . 还要注意,如果内存量太大,内存分配可能会失败,在这种情况下,返回的指针将为NULL

Technically, sizeof(char) above isn't needed because 1 char will always be 1 byte in size, but most other data types are bigger and the multiplication is important - malloc(100) allocates 100 bytes, malloc(100 * sizeof(int)) allocates the amount of memory needed for 100 int s which usually is 400 bytes on 32-bit systems, but can vary. 从技术上讲,不需要上面的sizeof(char) ,因为1个char始终为1个字节,但是大多数其他数据类型更大,并且乘法很重要malloc(100)分配100个字节, malloc(100 * sizeof(int))分配100 int所需的内存量,在32位系统上通常为400字节,但是会有所不同。

int amount = calculate_my_memory_needs_function();
int* buf = malloc(amount * sizeof(int));
if (buf == NULL) {
  // oops!
}
// ...
if (more_memory_needed_suddenly) {
   amount *= 2; // we just double it
   buf = realloc(buf, amount * sizeof(int));
   if (!buf) { // Another way to check for NULL
     // oops again!
   }
}
// ...
free(buf);

Another useful function is calloc that takes two parameters (first: number of elements to allocate, second: size of an element in bytes) and initializes the memory to 0. 另一个有用的函数是calloc ,它具有两个参数(第一个:要分配的元素数,第二个:元素的大小(以字节为单位))并将内存初始化为0。

我认为calloc(如果没有calloc,则为malloc)是您所追求的。

buf is an array statically allocated on the stack. buf是在堆栈上静态分配的数组。 For it to be of type char * you need to allocate dynamically the memory. 为了使其成为char *类型,您需要动态分配内存。 I guess what you want is something like this: 我想你想要的是这样的:

int main()
{
   int n = 56;
   char * buf = NULL;
   buf = malloc(10 * sizeof(char));
   sprintf(buf, "%i", n);
   printf("%s\n", buf);

   // don't forget to free the allocated memory
   free(buf);

   return 0;
}

EDIT: As pointed out by 'Thomas Padron-McCarthy' in the comments, you should not cast the return of malloc() . 编辑:正如“ Thomas Padron-McCarthy”在评论中指出的那样,您不应该转换malloc()的返回值。 You will find more info here . 您可以在此处找到更多信息。 You could also remove completely the sizeof(char) as it will always by 1 . 您也可以将sizeof(char)完全删除,因为它将始终为1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM