views:

149

answers:

2

The following are the two ways of initializing a char array:

char charArray1[] = "foo";
char charArray2[] = {'f','o','o','\0'};

If both are equivalent, one would expect everyone to use the first option above (since it requires fewer key strokes). But I've seen code where the author takes the pain to always use the second method.

My guess is that in the first case the string "foo" is stored in the data segment and copied into the array at runtime, whereas in the second case the characters are stored in the code segment and copied into the array at runtime. And for some reason, the author is allergic to having anything in the data segment.

Edit: Assume the arrays are declared local to a function.

Questions: Is my reasoning correct? Which is your preferred style and why?

+1  A: 

I think the second method is used mostly in legacy code where compilers didn't support the first method. Both methods should store the data in the data segments. I prefer the first method due to readability. Also, I needed to patch a program once (can't remember which, it was a standard UNIX tool) to not use /etc (it was for an embedded system). I had a very hard time finding the correct place because they used the second method and my grep couldn't find "etc" anywhere :-)

DarkDust
which compilers "didn't support the first method"? Its strictly C89, each ANSI compiler should support this.
@DarkDust: that's nice if you hold your char array to hold some text, if you want to store numbers `"\x66\x6f\x6f"` is definitely less readable than `{102, 111, 111, 0}` (you even have to go with hexa or decimal conversion because you can't type a char code using decimal). Loosing the second form would be a real pain in some cases.
kriss
DarkDust
@kriss: I wasn't saying anything like the second form would be a bad idea in general, but it's a bad idea if you really want to store a text. `{'/', 'e', 't', 'c', 0}` really IS less readable/findable than `"/etc"`, don't you agree ?
DarkDust
@DarkDust: I most certainly agree. I just wanted to point out that what is the best solution for some use case can become a problem with another use case. Use the style that fit what you are doing.
kriss
+4  A: 

What about another possibility:

char charArray3[] = {102, 111, 111, 0};

You shouldn't forget the C char type is a numeric type, it just happens the value is often used as a char code. But if I use an array for something not related to text at all, I would would definitely prefer initialize it with the above syntax than encode it to letters and put them between quotes.

If you don't want the terminal 0 you also have to use the second form or in C use:

char charArray3[3] = "foo";

It is a a C feature that nearly nobody knows, but if the compiler does not have room enough to hold the final 0 when initializing a charArray, it does not put it, but the code is legal. However this should be avoided because this feature has been removed from C++, and a C++ compiler would yield an error.

I checked the assembly code generated by gcc, and all the different forms are equivalent. The only difference is that it uses either .string or .byte pseudo instruction to declare data. But tha's just a readability issue and does not make a bit of difference in the resulting program.

kriss