I will try we are learning about this now
the char* ch is a char pointer to ch that has no value.
once you create an instance of ch (ch=new ???) you have a object that can hold a value
so them when you call the gets (ch) it has a place to put the string.
I am pretty new at this but i think that is what is happen
^^ Your right. "ch" needs to be initialized for it to point to a value place.
1 2 3 4 5 6 7 8 9 10
int main() {
char* ch = newchar[80]; // or what ever number you want.
for(unsignedint i = 0; i < 80; i++) {
// don't change the pointers value, that will cause memory leaks.
cout << ch[i]; // for one at a time or just the hole thing remove "[i]"
// cout can take the pointer as is, it can detect that this is a char array.
}
delete [] ch;
}
If you use a for loop like you did in your code, going from 0 to 79, it will display 'a' then anything that is on the 79 memory addresses after that. If you use the second one, stopping when *ch == '\0', it will display 'a' then more rubbish until a memory address happens to have the value '\0' in it.
No, when s/he has for(;*ch;ch++) first its comparing a Boolean operation if *ch equals something comparable to true. That means that it will likely never stop looping until it sees a NULL. Then s/he is ch++ the pointer so that now we're going into random memory (very dangerous!). That also makes deleting "ch" impossible unless you rewind the changes. I doubt that will run without crashing. By all means that is very bad.
As to why your not printing 'a' is probably because when the program asks for user input at gets(ch) it is deleting 'a' and replacing it with what ever the user types in.