0
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* ?

Innkeeper
  • 623
  • 1
  • 9
  • 21
  • 1
    When you do `char buf[10]` memory allocate statically you not need to de-allocate, But for `cher* buff` you have to do memory management explicitly. [Here is difference](http://stackoverflow.com/questions/13836509/character-string-pointer-vs-array/13836587#13836587) between two declarations – Grijesh Chauhan Dec 15 '12 at 18:54

3 Answers3

3

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. Also note that memory allocation can fail if the amount of memory is too large, in this case the returned pointer will be 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 ints which usually is 400 bytes on 32-bit systems, but can vary.

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.

schnaader
  • 46,785
  • 9
  • 98
  • 132
1

I think calloc (or malloc if you don't have calloc) is what you're after.

Minthos
  • 890
  • 4
  • 13
1

buf is an array statically allocated on the stack. For it to be of type char * you need to allocate dynamically the memory. 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(). You will find more info here. You could also remove completely the sizeof(char) as it will always by 1.

Community
  • 1
  • 1
koopajah
  • 19,437
  • 8
  • 62
  • 93
  • Thank You, I'm still pretty unconfortable with the manual memory management. – Innkeeper Dec 15 '12 at 18:49
  • No problem, I added the code to release the memory if you really are not used to this! @schnaader yeah I thought so too now that he says he is new to the concept – koopajah Dec 15 '12 at 18:51
  • 1
    No, buf isn't already a char*. An array is an array and a pointer is a pointer! – Thomas Padron-McCarthy Dec 15 '12 at 18:58
  • Hmmm Do you have a link to understand what you mean by that? I've read something similar the other day on another SO question but can't grasp why buf would not be considered of type `char *` even if it references a static array on the stack here. – koopajah Dec 15 '12 at 19:02
  • 1
    buf is _not_ "referencing" a static array, it _is_ a static array. It is not a pointer. See also http://stackoverflow.com/questions/1641957/is-array-name-a-pointer-in-c/1641963#1641963 – Thomas Padron-McCarthy Dec 15 '12 at 19:03
  • Ok, I've never heard of the concept of array which are converted to pointers implicitely when needed so thanks for pointing this out – koopajah Dec 15 '12 at 19:06
  • As a wise man said: Don't cast malloc, and sizeof(char) is always 1 – Thomas Padron-McCarthy Dec 15 '12 at 19:09
  • Why not cast malloc() ? I thought it was mandatory to cast the return of a malloc() call? And yes sizeof(char) is always 1 but what is the point of putting 1 hardcoded in the call when you can just be consistent and always use sizeof() of the expected type? I think it would be a mistake to remove the sizeof() in this case and more confusing than useful – koopajah Dec 15 '12 at 19:10
  • 1
    Both of those are "signals" that many C programmers will interpret as signs of an inexperienced programmer, whose code will have to be treated with suspicion. About the cast, also see http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Thomas Padron-McCarthy Dec 15 '12 at 19:14
  • Thanks for your link again, I'll have to dig up the emails I had in my previous company about why I must cast my calls and understand where I missed something (I did not cast at first but I guess wasn't sure of what I was doing). I still disagree on the point of `sizeof(char)`. I do not see why adding this would mean I'm an inexperienced programmer. It just seems easier to use the same call convention of malloc() everywhere instead of specific cases where I know the size of the type anyway. Same for someone reading my code or changing it later. – koopajah Dec 15 '12 at 19:17
  • It is not that it is a specific case where you happen to know the size of the type, but that sizes in C are measured in char, so by definition sizeof(char) is 1. But yes, there are also arguments _for_ using sizeof(char). – Thomas Padron-McCarthy Dec 15 '12 at 19:21
  • The more I answer here, the more I learn. Thanks a lot for pointing all this out and improving my understanding of C. – koopajah Dec 15 '12 at 19:22