Dean
Dean

Reputation: 6960

Why can I assign a string literal whose length is less than the array itself?

I'm a bit baffled that this is allowed:

char num[6] = "a";

What is happening here? Am I assigning a pointer to the array or copying the literal values into the array (and therefore I'm able to modify them later)?

Upvotes: 1

Views: 137

Answers (6)

songyuanyao
songyuanyao

Reputation: 172924

  1. Why can I assign a string literal less than the array itself? What is happening here?

This is well defined. When initialize character arrays with string literal,

If the size of the array is specified and it is larger than the number of characters in the string literal, the remaining characters are zero-initialized.

So,

char num[6] = "a";
// equivalent to char num[6] = {'a', '\0', '\0', '\0', '\0', '\0'};
  1. Am I assigning a pointer to the array or copying the literal values into the array (and therefore I'm able to modify them later)?

The value will be copied, i.e. the elements of the array will be initialized by the chars of the string literal (including '\0').

String literals can be used to initialize character arrays. If an array is initialized like char str[] = "foo";, str will contain a copy of the string "foo".

Successive characters of the string literal (which includes the implicit terminating null character) initialize the elements of the array.

Upvotes: 5

SloCompTech
SloCompTech

Reputation: 126

That is one type of declaration whcih is equivalent to

char num[6] = {'a','\0'};

You declared c-string with length of max. 5 normal chars, at the end must me \0 to end c - string.

With declaration you can use

char num[6] = "a";

then you need to assign value:

  1. With strcpy(dest,src)

    strcpy(num,"test");

  2. Char by char

    num[0]='t'; num[1]='e'; num[2]='s'; num[3]='t'; num[4]='\0';

Upvotes: 0

eerorika
eerorika

Reputation: 238351

Why can I assign a string literal less than the array itself?

This is allowed by the language. It is often useful to be able to add more characters to the array later, which wouldn't be possible if the existing characters filled the entire array.

Am I assigning a pointer to the array

No. You cannot assign a pointer to an array, so that is not happening.

or copying the literal values into the array

That is exactly what is happening.

and therefore I'm able to modify them later

You are able to modify the array, indeed.

Upvotes: 1

Paul Stelian
Paul Stelian

Reputation: 1379

This kind of declaration is a special syntax sugar thing. It's equivalent to

char num[6] = {'a', 0}

The array is always modifiable. Its contents after such a declaration would be a character representing 'a', a zero (NUL terminator) and the remainder of the array will also be zeroed (zero initialization).

Upvotes: 0

wilx
wilx

Reputation: 18228

Just use char num[6] = {"a"};. It works.

Upvotes: 0

Jarod42
Jarod42

Reputation: 217275

char num[6] = "a";

is equivalent to

char num[6] = {'a', '\0', '\0', '\0', '\0', '\0'};

Upvotes: 3

Related Questions