Search code examples
cpointerscharstrcmp

Using strcmp to compare a char pointer and a literal


I've been trying this for about an hour and looked at other places online but still was not able to accomplish what I'm trying to do. This should probably be something simple but I have not coded in C in quite awhile so I may be missing something. Anyway I want to use strcmp to compare something in a variable called char *args and a literal like "qw".

I am in main and pass a function this args variable and when I get it back I want to compare like this: Maybe I am just completely doing this in a dumb way.

char *args;
char **svdArray;

int keepTrack = 0;
int beforeTrack = 0;

svdArray = malloc(5*sizeof(char*));
while (1)
{           

if(!(strcmp(args[0], "r")) && (!strcmp(args[0], "rr")))
  {

    svdArray[keepTrack] = args[0];


     keepTrack++;
  }

All I want to happen is if I args[0] has something in it besides rr or r I want to execute the code inside the if statement. However as of now the flow just never enters it and I don't know why.

Any help would greatly be appreciated!!!


Solution

  • strcmp() compares both strings in whole.

    To only test for parts use strstr() like for example so:

    if (strstr(args, "r")) || strstr(args, "rr")))
    {
      svdArray[keepTrack] = args;
      keepTrack++;
    }