#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char str[80] ;
char token[80] ;
char *p, *q ;
cout << "Enter a sentance: ";
gets(str) ;
p = str ; // p points to str[80] address
while(*p) // kinda stuck(loop while *p != 0)
{
q = token ; // q points to token[80] address
while(*p != '' '' && *p) // loop while *p != space and *p != 0
{
*q = *p ; // set value of q = p
q++, p++ ; // points to the next element
}
if (*p) // stuck here!! HELP!
{
p++ ; // go to next element
*q = ''\0'' ;
cout << token << ''\n'' ;
}
}
return 0 ;
}
ok, im stuck. ok in the first while loop...i loop while *p(or the value) of p =! NULL, right?? ok in the second loop im also stuck. if(*p) which means if NULL or 0, right?? and why is *q = ''\0'' after i go to the next element? wont that ''end'' the array?? HELP! this is confusing! sorry if im confusing you too! im trying to get this pointer stuff down any step by step explanation will be great. this book didnt do a god job of explaining this example...!
argh! i need some help with this smaller pointer problem
Try This,
if (*p)
p++ ; // go to next element
if (q != token)
// stuck here!! HELP!
{
*q = ''\0'' ;
cout << token << ''\n'' ;
}
if (*p)
p++ ; // go to next element
if (q != token)
// stuck here!! HELP!
{
*q = ''\0'' ;
cout << token << ''\n'' ;
}
Try using only one word for the data. E.g.,
asdasd
instead of
asdasd asdasd
it skip the last entry.
As an explanation,
if (*p) // stuck here!! HELP!
means
if(the value at p != 0)
and
while(*p) // kinda stuck(loop while *p != 0)
What eles do you need explaning for?
means the same thing
asdasd
instead of
asdasd asdasd
it skip the last entry.
As an explanation,
if (*p) // stuck here!! HELP!
means
if(the value at p != 0)
and
while(*p) // kinda stuck(loop while *p != 0)
What eles do you need explaning for?
means the same thing
Ok, here''s a brief explanation of what''s happening:
The first while loops around as long as the character p points to isn''t 0, or the string terminator.
q is then set to point at token[0].
The second while then loops around as long as *p isn''t a white space or 0. Then *q is set to *p, and both pointers are incremented by one, setting them to point at the next character in their respective arrays (ie, in the first iteration, q will be set to point at token[1], and p to str[1]). So, q[0] will be set to p[0], then q[1] will be set to p[1], etc.
After the second while loop exits, the program checks whether it exited because *p was '' '' or 0. If it was white space, it increments p again, so it skips the white space, and null terminates token. It does this to turn token into a null-terminator char array, so ''cout'' doesn''t overflow.
The cycle continues as long as p still isn''t pointing at the null terminator. q is set to point at token[0] again, over-writing the previous characters stored there.
If you can''t get your head around it, let me know and I''ll draw a diagram or something. I know how difficult it is to understand pointers, but in a couple of years you''ll love ''em!
Pete
The first while loops around as long as the character p points to isn''t 0, or the string terminator.
q is then set to point at token[0].
The second while then loops around as long as *p isn''t a white space or 0. Then *q is set to *p, and both pointers are incremented by one, setting them to point at the next character in their respective arrays (ie, in the first iteration, q will be set to point at token[1], and p to str[1]). So, q[0] will be set to p[0], then q[1] will be set to p[1], etc.
After the second while loop exits, the program checks whether it exited because *p was '' '' or 0. If it was white space, it increments p again, so it skips the white space, and null terminates token. It does this to turn token into a null-terminator char array, so ''cout'' doesn''t overflow.
The cycle continues as long as p still isn''t pointing at the null terminator. q is set to point at token[0] again, over-writing the previous characters stored there.
If you can''t get your head around it, let me know and I''ll draw a diagram or something. I know how difficult it is to understand pointers, but in a couple of years you''ll love ''em!
Pete
quote:
After the second while loop exits, the program checks whether it exited because *p was '' '' or 0. If it was white space, it increments p again, so it skips the white space, and null terminates token. It does this to turn token into a null-terminator char array, so ''cout'' doesn''t overflow.
i am totally confused there. how does "if(*p)" know its a whitespace? and secondly...how will cout overflow? thanks again!
All the C string functions assume your character arrays are null terminated, ie, the last character is ''/0'' (also just the number 0 - this is why the checks like ''while (*p)'' work). If the terminator isn''t there, it doesn''t know where the string stops and will overflow the array.
The whitespace check is done in the line:
while (*p != '' '' && *p)
Pete
The whitespace check is done in the line:
while (*p != '' '' && *p)
Pete
why are you using gets()? You''re using C++ IO, so just use cin.
cin>>str;
this will also allow you to elliminate the file. Otherwise, practices like this will eat up hardrive space.
cin>>str;
this will also allow you to elliminate the file. Otherwise, practices like this will eat up hardrive space.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement