Reputation: 8424
Let's say you have:
char[5] = "March";
this won't work in C although:
char[0]='M'
char[1]='a'
char[2]='r'
char[3]='c'
char[4]='h'
char[5]='\0'
For some reason, I have to say to C that char[6]="March"
and not char [5]
.
Why is this? What goes into char[6]
?
Upvotes: 1
Views: 163
Reputation: 882426
First things first, char
is not a valid variable in C, it's a keyword. You probably meant
char xyzzy[6];
or something similar, which would create a character array called xyzzy
. But, once that's fixed up, nothing goes into xyyzy[6]
. The statement char xyzzy[6];
means an array of six characters, the indexes of which are 0
through 5
inclusive: {0,1,2,3,4,5}
, that's six elements.
In any case, unless you need the array to be bigger, you're usually better off letting the compiler choose the size with:
char xyzzy[] = "March";
Upvotes: 4
Reputation: 21
Char
is a keyword not a variable name, so don't use it as one.
Array indexing: that is counting the number of characters in an array begining from 0, i.e a[0].
Suppose there are 5 characters in the array (for eg: tiger) then a[0] = 't' and a[4] = 'r' and all the string literals in this case an array of characters ends in a '\0' i.e a EOF
character. So to store an array of n characters use a[n] compiler will add '\0' at the end of that array.
Upvotes: 0
Reputation: 122001
A string literal, "March"
in this case, has an implicit null terminator so a char[]
array requires 6
elements to store it. To quote the relevant points from the section 6.4.5 String literals of the C99 standard:
A character string literal is a sequence of zero or more multibyte characters enclosed in double-quotes, as in "xyz".
In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals.
In both cases the code is overrunning the end of the array. The code currently has undefined behaviour.
Change to:
char a[6] = "March";
The second code snippet accesses beyond the end of the array as indexes run from 0
to N - 1
, where N
is the number of elements in the array. For an array char a[5];
the valid index are 0, 1, 2, 3, 4
.
Upvotes: 1
Reputation:
char array[n]
means an n elements long array, not an array whose last index is n
. Because in C, arrays are zero-based, it means that the last valid index of an n-element array is n - 1
. Since there's a zero terminator for a string, and "March" is 5 letters, that's a total of 6 characters, so you have to write char str[6] = "March";
. If that's confusing for now, don't include the length of the array; for initialized arrays, the compiler will automagically fill it in, so you can write char str[] = "March";
instead.
Upvotes: 0
Reputation: 7277
Nothing goes into char[6], but you still need a char[] of size 6 to hold the 6 characters including the null-terminator byte. They will go into char[0] through char[5]. char[6] remains undefined.
Upvotes: 0
Reputation: 9740
char [5]
means, you have memory for 5x sizeof(char). 0-5 are six times sizeof(char): 0 first, 1 second, 2 third, 3 fourth, 4 fifth, 5 sixth. So you need a segment with 6 times sizeof(char): char [6]
. The first cell, zero, consumes space, too.
Upvotes: 0
Reputation: 382444
There are 6 elements if you count from 0 to 5 included.
So you must declare a char[6]
in order to have str[0]
to str[5]
.
Upvotes: 0
Reputation: 1929
The null terminator \0 goes in the last slot. It is there because otherwise there would be no way to check the length of the string.
Upvotes: 0