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.
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.
Last edited by eXeCuTeR; 11-26-2007 at 05:44 AM.
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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";
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Great, thanks.