String array is not reading in the value? [closed]
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this questionIn the Second while loop the data in string is there i checked "data added" however when i stepin and check what string[walker + *len] it constantly has NUL ('\0') even after len is increment? What is the error here ?!?
char* getWord(char* string, short* len)
{
size_t walker = 0;
/*POINT TO THE FIRST CHAR*/
while (string[walker] == ' ' ||开发者_如何学编程 string[walker] == '\0')
++walker;
while ( string[walker + *len] != ' ' || string[walker + *len] != '\0' )
++(*len);
return (&string[walker]);
You have a few logic bugs there - it should be something like this:
char* getWord(char* string, short* len)
{
size_t walker = 0;
*len = 0; // << initialisation of *len
/*POINT TO THE FIRST CHAR*/
while (string[walker] == ' ') // << remove incorrect check for end of string
++walker;
while (string[walker + *len] != ' ' && string[walker + *len] != '\0') // << fix logic for testing for space or end of string
++(*len);
return &string[walker];
}
Are you sure that *len
is 0 when you call your function? Don't you need to initialize it? Also, in the second while condition I'd guess that you want to use a &&
rather than ||
.
精彩评论