+ 4
focus on programming... what is the problem of this code?
char s[] ="this is a character array"; for( ; *s != '\0' ; ++s) cout<< *s << ' ';
3 odpowiedzi
+ 1
arrays are const pointers(when we use [ ] we make const pointer), so we cannot make any changes in their address, so we can't do ++c...
so we shout do sth like this:
char *s ="this is a character array";
for( ; *s != '\0' ; ++s)
cout<< *s << ' ';
+ 1
Your variable s is a pointer. You assign its address to a part of memory that holds your string. But this string is a rvalue, which means it disappears after the execution of the code line. Therefore the memory zone is not supposed to hold your string anymore (you may see it as automatically fred from the stack by the compiler), your pointer s is dangling and using it causes undefined behavior as per the c++ standard. PS: sorry for my English.
0
We aren't in Java Q&A Forum?