Colin Fausnaught
Colin Fausnaught

Reputation: 323

NULL values and "0" strings in C?

I'm having trouble adding a "0" string to a list of strings in C.

With my current code, I'm having trouble distinguishing between a NULL value and a "0" with my current code. I have tried to debug with the if statement (if(pch == "0")) but when I'm outputting it doesn't go into the if statement.

          printf("%c\n",next_line[0]);
          pch = strtok(next_line,",");
          printf("%s\n",pch);
          if(pch == "0"){
            printf("NULL VALUE");
            strcpy(strs[i],pch);
            i++;
            pch = strtok(NULL, ",");
          }
          while(pch != NULL){
            strcpy(strs[i],pch);
            i++;
            pch = strtok(NULL, ",");
          }
          //go on to print values

the input is: 0,1,03:48:13,9 I never make it past the first character. What can I do to keep my code? Can I maybe change

     pch = strtok(NULL, ","); 
    to pch = strtok(SOMEOTHERVALUE, ",");? 

Upvotes: 1

Views: 80

Answers (1)

Karthikeyan.R.S
Karthikeyan.R.S

Reputation: 4041

You cannot able to check like this,

if(pch == "0"){
 ...
}

pch is pointer. You are comparing that with a string. So you have to make that as like this,

if ( *pch == '0' ) { 
 ...
}

Or else do like this,

if ( strcmp(pch,"0") == 0 ) {
 ...
}

It will help you to check the zero in a character string.

Upvotes: 2

Related Questions