More C help Needed :)
The below Piece of code is what im haing probs with
char string[80], *p;
int count;
p = string;
printf (" Please enter a string... ");
gets(p);
while(p != " ")
{
printf ("%c", p);
p++;
}
Mainly the while loop, how would i stop the loop when a space character is found? I thought that''d work but it doesnt
I know i dont need pointers to do somin like this but im learning pointers so....
Thanks in advance...
Hi there,
My C is a little rusty, but here goes...
Your variable ''p'' is a ''pointer to char'', not a ''char''.
The expression in your while loop says ''while the null-terminated string starting a position p is not the single-character string SPACE''. What you want to say is ''while the character at position p is not the character SPACE''.
So you need to dereference p to get at the character pointed to by p, something like:
while (*p != '' '')
Note that this now compares ''the char pointed to by p'' to ''the char SPACE'', rather than ''the null-terminated string p'' to ''the string SPACE''.
Hope this helps.
My C is a little rusty, but here goes...
Your variable ''p'' is a ''pointer to char'', not a ''char''.
The expression in your while loop says ''while the null-terminated string starting a position p is not the single-character string SPACE''. What you want to say is ''while the character at position p is not the character SPACE''.
So you need to dereference p to get at the character pointed to by p, something like:
while (*p != '' '')
Note that this now compares ''the char pointed to by p'' to ''the char SPACE'', rather than ''the null-terminated string p'' to ''the string SPACE''.
Hope this helps.
I think the problem lies with your while loop
you should do something like this
You need to dereference the pointer (note the * before p). What you were doing was checking the address of the pointer, not the value that the pointer pointed to.
Oh yeah, 20 is the ASCII code for a space. I think thats what you need.
That should solve your problem
========================================================
If something sounds stupid but works, it's not stupid
Edited by - NuffSaid on June 13, 2000 8:53:15 AM
you should do something like this
while(*p != 20){ //do your stuff here}
You need to dereference the pointer (note the * before p). What you were doing was checking the address of the pointer, not the value that the pointer pointed to.
Oh yeah, 20 is the ASCII code for a space. I think thats what you need.
That should solve your problem
========================================================
If something sounds stupid but works, it's not stupid
Edited by - NuffSaid on June 13, 2000 8:53:15 AM
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Hi
Try *p this will give you the character witch is in/at/under (don''t know the wright preposition) the adress saved in p. Further more you have to look for '' '' not " " because you are looking for an char not a string. You can do the following:
for(int count=0; string[count] != '' ''; ++count){
printf("%c", string[count]);
}
I hope I have helped you and didn''t tell you anything wrong I''m a newbie too.
Try *p this will give you the character witch is in/at/under (don''t know the wright preposition) the adress saved in p. Further more you have to look for '' '' not " " because you are looking for an char not a string. You can do the following:
for(int count=0; string[count] != '' ''; ++count){
printf("%c", string[count]);
}
I hope I have helped you and didn''t tell you anything wrong I''m a newbie too.
May the force be with you
Two others were faster, but this itn''t the reason for this post, the reason is:
How did NuffSaid include this nice ''code window''? It''s much better than the code examples with this small not readable letters.
How did NuffSaid include this nice ''code window''? It''s much better than the code examples with this small not readable letters.
May the force be with you
>> How did NuffSaid include this nice ''code window''? It''s much better than the code examples with this small not readable letters. <<
Put all source code between [ source] [ /source] tags.
/. Muzzafarath
Mad House Software
Put all source code between [ source] [ /source] tags.
int void double this
/. Muzzafarath
Mad House Software
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
NuffSaid said the space''s ASCII code value was 20. In hexadecimal! The decimal value being 32, in the code sample he provided you''d use the 0x20 value!
just one more thing: gets() is evil, use fgets or scanf.
gets() leads to buffer overflows, buffer overflows leads to crashing, crashing leads to suffering.
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
gets() leads to buffer overflows, buffer overflows leads to crashing, crashing leads to suffering.
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement