Reputation: 11966
Consider the two following lines of code:
const char *ptr = "Hello";
char arr[] = "Hello";
For the pointer definition, the "Hello"
string literal is essentially immutable, but the ptr
variable itself can change and hold a different address.
For the array definition, the "Hello"
string literal is copied to the location of the array, but arr
cannot point to a different location; however, the string held by the array is mutable and thus can be changed.
Now consider the following two lines of code:
const char * const ptr = "Hello";
const char arr[] = "Hello";
Here, both strings are immutable as a result of the const char
qualifier -- more interesting though: with ptr
defined as a constant pointer, it cannot point to a different address.
Would these two lines of code result in the same behavior? If the end effect is the same, is there a theoretical difference in implementation -- for example, does the pointer method allocate memory for an anonymous array to hold the string in addition to the pointer itself, while the array method only allocates memory for an array?
Upvotes: 3
Views: 3333
Reputation: 2881
For the array definition, the
"Hello"
string literal is copied to the location of the array, but arr cannot point to a different location
The string is not copied into the array, it's not created and stored in a read-only location like string literals (that can be implicitly converted to pointers), but it's just a shorthand for
char arr[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
As the standard says:
An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces.
And arrays don't "point", arrays are not pointers.
Would these two lines of code result in the same behavior?
No, the have completely different types, for the same reason.
does the pointer method allocate memory for an anonymous array to hold the string in addition to the pointer itself, while the array method only allocates memory for an array?
Yes, and the "anonymous" created array could be used again in another place if the same string literal is used (but in the general case you can't know what the compiler actually does):
It is unspecified whether these arrays are distinct provided their elements have the appropriate values.
Again, the second line is just synctactic sugar to initialize the array.
Upvotes: 1
Reputation: 5029
const char *ptr = "Hello";
This declares ptr as a const pointer-to-char, initialized to point at the string literal "Hello". What is pointed at does NOT have a const type, although the implementation is permitted to place it in read-only memory.
char arr[] = "Hello";
This declares arr as array[6] of char, initialized to {'H', 'e', 'l', 'l', 'o', '\0'}
.
const char * const ptr = "Hello";
This declares ptr as a const pointer-to-const-char, initialized to point at the string literal "Hello". Although this pointer is declared as pointing at const memory, the string literal itself still does not have const type, although the implementation is still permitted to place it in read-only memory.
const char arr[] = "Hello";
This declares arr as a const array[6] of char, again initialized to {'H', 'e', 'l', 'l', 'o', '\0'}
.
All different, but I see that others have already provided better answers to your question.
Upvotes: 3
Reputation: 78903
There is a difference between the two: their address. All string literals with the same contents may (but must not necessarily) point to the same adress. The array definition, if it is in function scope defines a new object, different from the string literal and also different from any other such object with the same contents. Thus the addresses must be different.
This will in particular be the case if your function is recursive. Then all nested invocations of your function would define a new variable, each with a different adress.
Upvotes: 3
Reputation: 53017
Here's a few differences.
First off, this may hold true on certain implementations as the pointers can point to the same memory:
const char * const ptr1 = "Hello";
const char * const ptr2 = "Hello";
ptr1 == ptr2;
But it cannot be true using the array form.
Anyway, the real difference is that their types are different. In particular, the char[]
version retains its size in the array type. So sizeof(arr)
gives you the size of the array, rather than a pointer, and you can also create pointers to arrays to arr
.
Upvotes: 4
Reputation: 224844
Well, there is at least one semantic difference in that &ptr
and &arr
will yield different types - one a pointer-to-pointer and the other pointer-to-array. Whether that actually matters anywhere will of course depend on how you use them. Check the output machine code from your compiler for specific results.
Upvotes: 2