p && *p !='$' I don't understand why p is so important to this test!<Resolved>
Hello everyone, I was looking at some code, and this throws me off, i have no idea why if I delete p and just leave *p!='$'; it will crash the whole program. here's the code:
Code:
void tokenize(char **name, int &n, char * buffer)
{
char *p = strtok(buffer, "\n");
for(n = 0; p && *p !='$'; n++)
{
name[n] = p;
p = strtok(NULL, "\n");
}
}
here's some info about the values being passed:
Code:
const int NAME_LENGTH = 20;
const int MAX_NUM_NAMES = 25;
const int BUFFER_LENGTH = MAX_NUM_NAMES* (NAME_LENGTH + 1);
void input(char * buffer);
void tokenize(char **name, int & numNames, char* buffer);
void print(char **name, int numNames);
void sort(char ** name, int numNames);
int main(void)
{
char * name[MAX_NUM_NAMES];
char buffer[BUFFER_LENGTH+1];
int numNames;
input(buffer);
tokenize(name,numNames,buffer);
print(name,numNames);
sort(name,numNames);
print(name,numNames);
return 0;
}
If someone could explain to me what exactly p && *p !='$'; evalutes that would be great!
also, is it a good idea to use char ** or is there a better way to appraoch this? This isn't my code, its code out of a book and i've never seen it put this way thats why i'm so concerned!
-Thanks:D