char buffer[25];
Declares a variable called
buffer
, and allocates it 25 bytes of stack space.
char *buffer;
declares a variable called
buffer
and allocates it 4 bytes of stack space. If it has a value, it is zero.
So when you use it:
sprintf( buffer, "Sum of %d and %d is %d", a, b, c);
The first one passes the address of the first element of the array to the
sprintf
function (because the name of an array is a pointer to the first element by definition in the C language specification).
The second one at best passes a pointer to "zero memory" and at worst a random value as the pointer.
And your app crashes.
Now, if you'd done the second one like this:
char *buffer = (char*) malloc(25);
It would have worked.