I'm new in c++ and have come across a problem which I don't know what to make of. I was doing some examples in a book when I came across the following piece of code.
for(int i=0;string[i];i++){
cout<<string[i];
}
Here instead of a boolean for the second argument the person has given the array index.Based on that method I ran the following piece of code but it didn't work.
char string[50] ="This is a test!";
for(int i=sizeof(string)-1;string[i];i--){
cout<<string[i];
}
cout<<"\n";
I debugged it and the 'string[i]' has the value of 0. So my questions are,
- Why use an array index instead of a boolean (is it OK?).
- Why didn't my second piece of code work.
Thanks in advance.