4

To be clear, I'm not asking the difference between char *str and char str[]. I'm even more of a beginner than that. I'm implementing the following code:

// Convert long long to string 
    char number_str[];
    sprintf(number_str, "%lld", number_ll);

When I run printf("%s\n", number_str);, it spits out the correct number, no matter if I put 2 or 256 between those brackets. Which leads me to ask, what are these brackets for?

I was exposed to char str[] in this post.

Community
  • 1
  • 1
Crowder
  • 187
  • 6

2 Answers2

3

When I run printf("%s\n", number_str);, it spits out the correct number, no matter if I put 2 or 256 between those brackets. Which leads me to ask, what are these brackets for?

This is because your code invokes undefined behavior. You have not allocated space for your array.

What is the meaning of the value placed between the braces in char str[]?

It means that how much space do you want to allocate for your arrays. But note that when an initializer is used, this value, i.e, length of array (first dimension in case of multidimensional arrays) can be omitted.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • Generally it is in bytes (words). – haccks Jan 18 '14 at 23:59
  • Will it accept a variable? (By the way, if there's some documentation I should be looking at in order to be a more responsible member of this community, please just link it. I don't want to abuse the help I'm getting here.) – Crowder Jan 19 '14 at 00:01
  • Yes. Once you declared an array then you can use variable as index. C99 also allow [`VLAs`](http://en.wikipedia.org/wiki/Variable-length_array). – haccks Jan 19 '14 at 00:04
  • *(By the way, if there's some documentation I should be looking at in order to be a more responsible member of this community, please just link it. I don't want to abuse the help I'm getting here*: Read this: http://stackoverflow.com/tour and this: http://stackoverflow.com/help. It will help you in the future. Best of luck :) – haccks Jan 19 '14 at 00:29
  • 2
    @haccks: It is in *elements* which, in this case, is bytes. And a WORD is not necessarily a byte (and very rarely is these days). – Ed S. Jan 19 '14 at 03:20
2

In an array declaration, the number between the [ and ] specifies the number of elements in the array.

The code in your question:

char number_str[];
sprintf(number_str, "%lld", number_ll);

is illegal. If it appears inside a function definition, then you cannot use empty brackets; you must specify the size of the array -- and a call to sprintf, or to any other function, can only appear inside a function definition.

If you do specify the size of the array:

char number_str[100];
sprintf(number_str, "%lld", number_ll);

that creates an object number_str which is an array of 100 char elements. If you make the array too small:

char number_str[3];
sprintf(number_str, "%lld", number_ll); /* let's say number_ll == 12345 */

then your program may appear to work, but in fact its behavior is undefined. The sprintf call will attempt to store 6 characters in a 3-character array.

A C compiler is not obliged to diagnose this error. It will very likely let you try to do this, resulting in data being written into memory areas that you don't own.

At file scope, you can declare something like:

extern char array[];

which specifies that an object called array, of type array (of some unspecified size) of char is defined elsewhere.

As a (rather annoying) special case, a function parameter defined with the syntax of an array is actually a pointer:

void func(char param[42]) {
    /* param is a pointer; the 42 is silently ignored */
}

Always remember that arrays and pointers are two different things. For an explanation of their admittedly confusing relationship, see section 6 of the comp.lang.c FAQ.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631