for loop's condition with char

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
	char ch[80];
	gets(ch);
	for(int i = 0;ch[i] != '\0';i++)
	{
		cout << ch[i];
	}
}


With the code above, it gets whatever I input and then output what it got.

When I changed a bit :

1
2
	char* ch;
	gets(ch);


It doesn't allow me to input,but when I use function that use the variable ch it just say ch is being used without being initialized ...

1
2
3
4
5
6
7
8
9
10
int main()
{
	char* ch;
	ch = new char('a');
	gets(ch);
	for(;*ch;ch++)
	{
		cout << *ch;
	}
}


and when I use the line ch = new char('a'); and full program above , it's ok but without it,the error exists...Plz explain on this point.
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
char ch[80]
is declaring a char array of size 80.

char * ch
is declaring a pointer to char (an address in memory).

ch = new char('a');
is allocation memory for a single char, copying the value 'a' to it and making ch point to that space in memory.

The second and third are not the way you use to create a dynamic array.

ch = new char[80];
is how you do it. But remember you have to explicitly delete it when you're done, or you will have a memory leak.

And you don't need a loop to print a character array, you can just use cout << ch;.
closed account (43RGz8AR)
^^ 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 = new char[80]; // or what ever number you want.
	for(unsigned int 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;
}
Last edited on
Yes, I got the point :

char ch[80];

and

char *ch = new char[80];

are now the same.

but when using ch = new char('a'); it doesn't output the 'a'.

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int main()
{
	char* ch;
	ch = new char('a');
	gets(ch);
	for(;*ch;ch++)
	{
		cout << *ch;
	}
}


If you compile that,it just have the same output as the corrected code. No 'a' is outputted. That's my confusion.
closed account (43RGz8AR)
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.
Yep I know it's bad just try some new way, thank you, solved.
closed account (43RGz8AR)
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.
Topic archived. No new replies allowed.