tags:

views:

140

answers:

3

What is the difference between these two forms of a string variable in C language?

char *string1;
char string2[];

Is there any other way to do it?

Thank you very much.

A: 

1) *char string1; is a pointer to (possibly) a string whereas

2) char string2[]; is more explicit in that there is an intention to point to an array

Either way, you still need to allocate memory to hold the said string. I would go with #2 from an elegance point of view.

jldupont
+7  A: 

char *string1 = "foo";

string1 is a pointer to a string literal (for the sake of argument, it points to a series of characters stored in a read-only data segment of the program).

char string2[] = "foo";

string2 is an array of 4 characters on the stack. It is initialised with the bytes 'f', 'o', 'o', ASCII_NUL.

Probably the most significant difference is that if you do string1[0] = 'b'; you get undefined behaviour, because you're trying to modify the stored representation a string literal. If you do string2[0] = 'b'; then you modify your personal string on the stack to "boo", which is fine.

In general, a variable of type char* is a pointer to a char. It's often used to point to the first char in a NUL-terminated sequence of chars, in which case it points to a string. A variable of type char[] is an array of chars. If it has a NUL terminator, then it actually is a string.

The issue is slightly confused by two facts:

1) In C, whenever an array variable name is used in a context that takes a pointer, it "means" a pointer to the first element of the array. So arrays and pointers are often thought to be interchangeable.

2) In C, a function parameter of type char[] is not in fact an array. It's just a pointer, exactly the same as char*. So, again, arrays and pointers are often thought to be interchangeable.

So, another difference between the pointer and the array:

string1 = "bar"; // changes string1 to point to another string literal.

string1 = string2; // changes string1 to point to the first character of string2.

string2 = string1; // doesn't compile - you can't assign to an array,
                   //   only initialize it and then modify element-by-element.

[Note: the statement char string2[]; in the question is not valid C syntax in a function, but I've assumed you're talking about variable definitions in a function and invented some definitions which are valid. The definitions I've used would be valid in file scope too, outside any function. In that case they're global variables, not on the stack, but otherwise behave as I've described for stack variables.]

Steve Jessop
+3  A: 

They are different in the way of the internal representation, but almost similar to way of working for the programmer.

While char string2[]; is an array of characters, when assigned directly, the compiler will understand that it's a collection of characters, making sizeof() return the size of the array (and not the string).

char *string1 is a pointer to the first character in a string. It contains no compile time information about what it holds and will return the size of a pointer (4 on 32bit, 8 on 64bit) when queried with sizeof. Pointers posses built-in operator[] (overridable in C++) that make them act as arrays.

Pointers are more flexible and can change to what content they point to, while arrays cannot. The only way you can fill arrays is by using strcpy, memcpy or similar manual copy. Pointers can be freely assigned to the first and even Nth element of any memory location to have more flexibility over the content.

LiraNuna