this array is being sorted to:Code:char arr[4] = { "a", "b", "c", "d" };
a = arr[0],
b = arr[1],
c = arr[2],
d = arr[3],
and \0 = arr[4].
I know I'm wrong, but I just don't understand where \0 goes to.
Printable View
this array is being sorted to:Code:char arr[4] = { "a", "b", "c", "d" };
a = arr[0],
b = arr[1],
c = arr[2],
d = arr[3],
and \0 = arr[4].
I know I'm wrong, but I just don't understand where \0 goes to.
That is wrong.
You have an array of char that is 4 long. Into it, you are trying to put pointers to the strings "a", "b", "c" and "d", each of which contain one letter and one NUL character.
If you meant to use single quotes, you still got it wrong, as the array only has four entries. There is no arr[4] - it is the "fifth element of a four element array", and as such is definitely in the "undefined" section of programming - what it "contains" is very much depending on what the surrounding data is, and it could contain literally any value.
--
Mats
Well if you'd written something which will compile, say this
then there would be no \0 at the end of the stringCode:char arr[4] = { 'a', 'b', 'c', 'd' };
Neither does this have a \0
Code:char arr[4] = "abcd";
So when there's a \0?
When you have a text string and there's enough space in the array.
E.g.
Just some variatins on the theme.Code:char arr[5] = "ABCD";
char arr[] = "ABCD";
char arr[] = { 'A', 'B', 'C', 'D' };
char *str = "ABCD";
--
Mats
If I have something like this, what will happen? 2 \0s?
e.g:
Code:char arr[6] = "ABCD";
Yes, there will be "enough zero's to fill the array", because ALL initialized data is "filled with zeros for anything that you haven't specifically defined".
--
Mats
Great, thanks.