Results 1 to 5 of 5

Thread: p && *p !='$' I don't understand why p is so important to this test!<Resolved>

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Thumbs up 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
    Last edited by voidflux; Jun 16th, 2003 at 01:27 AM.
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width