1

Given that these are allowed:

char c[] = "abc";
char c[] = {'a','b','c','\0'}
char *c = "abc";

Why then is this not allowed?

char *c = {'a','b','c','\0'};  // error: a char cannot initialize a char*

If a string literal is interpreted by the compiler as a null-terminated array of chars then I would have thought it equivalent to statement 3 above, no?

Similarly, why is this not allowed :

int arr[3];
int *i = arr; // OK
int *i = {1,2,3} // error: an int cannot initialize an int*

It's not a huge problem, I'm just curious.. I guess the question boils down to what's the difference between a string literal (or declared integer array) and an array of character elements when assigning to a pointer?

1

3 Answers 3

9

Arrays aren't pointers. Initializing a character array with a string literal is a special case (6.7.8#14):

An array of character type may be initialized by a character string literal, optionally enclosed in braces. Successive characters of the character string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.

and also (6.7.8#16):

Otherwise, the initializer for an object that has aggregate or union type shall be a brace-enclosed list of initializers for the elements or named members.

As to the pointer initialization (6.7.8#11):

The initializer for a scalar shall be a single expression, optionally enclosed in braces. The initial value of the object is that of the expression (after conversion); the same type constraints and conversions as for simple assignment apply, taking the type of the scalar to be the unqualified version of its declared type.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I will consult the language specification to learn more.
0

Arrays are not pointer. Just name of an array is a pointer to the 1st array item.

What you are asking here is the different method of initializing array in C/C++. Please note:

  1. Arrays can be initialized only on declaration line
  2. There is no String type in C/C++
  3. Array of chars are used as string in C/C++.
  4. Initialization of array of chars have the same rule of other arrays.
  5. If we define an array without explicit length, it will be defined with the length of number of initial values.
  6. If you don't define initial value, the items would be undefined. It means the array item's value are not clear initially.
  7. if you have an array and assign initial value to the 1st of afew items, remaining items will be 0.
  8. I will complete this list later ;-)

Comments

-2

String literal is a special case of unnamed data in section DATA. With arrays, you should handly allocate space on the heap, and move the data to there. (One-by-one, since you cannot assign arrays.)

3 Comments

String literals are often in the text section. And arrays can be on the stack. I don't understand this answer.
About string literals, I didn't know that, and the important part are that it's unnamed data that you get a pointer to it. (against {1,2,3}, which can be used only to init array). local arrays are in the stack.I guessed that he want that the value of the pointer will be valid even after the function.
Ok, I get it.. a string literal returns a valid pointer whereas assigning an undeclared array (e.g. {1,2,3} to a pointer in invalid as it does not exist to give us the pointer to it. Thanks guys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.