0

I am trying to write a program in c using array pointers...

My goal is to print the error message if the header values are same..will get the header values via command line argument.

If I get the same header Values, ie)(GET and GET) or (HEAD and HEAD),it should print valid and for all the other cases,it should print Invalid.

The program which i have written prints Invalid for all the combinations .i don't understand where i'm making making mistake.

int main(int argc,char *argv[])
{
        char *str1[4] = { "GET","HEAD","POST","OPTIONS"};
        char *str2[2] = {NULL};
        char *string = argv[1];
        const char s[2] = ",";
        char *token = "";

        int i = 0,j = 0,k = 0,l = 0,m = 0;

        token = strtok(string, s);
        while( token != NULL )
        {
              if(i < 2)
              {
                str2[i] = token;
                //printf( " %s\n", token );
                printf("str2[%d]= %s\n",i,str2[i]);
              }
              i++;
              token = strtok(NULL, s);
        }
        for(l = 0;l < 4;l++)
             printf("str1[%d] = %s\n",l,str1[l]);

        for( m = 0; m < 2;m++)
             printf("str2[%d] = %s\n",m,str2[m]);


        for(j = 0; j < 4;j++)
        {
                for(k =0; k < 2;k++)
                    {
                        if(str2[k] != str1[j])
                                printf("Invalid :: %s  and %s\n",str2[k],str1[j]);
                        else
                                printf("Valid :: %s and %s\n",str2[k],str1[j]);
                }
        }
        return 0;
}

compiling the code and getting output like this

bash-3.2$ gcc -o  arraypointer arraypointer.c
bash-3.2$ ./arraypointer GET,HEAD
str2[0]= GET
str2[1]= HEAD
str1[0] = GET
str1[1] = HEAD
str1[2] = POST
str1[3] = OPTIONS
str2[0] = GET
str2[1] = HEAD
Invalid :: GET  and GET
Invalid :: HEAD  and GET
Invalid :: GET  and HEAD
Invalid :: HEAD  and HEAD
Invalid :: GET  and POST
Invalid :: HEAD  and POST
Invalid :: GET  and OPTIONS
Invalid :: HEAD  and OPTIONS

But the expected output is

If I get the same header Values((GET and GET) or (HEAD and HEAD),it should print valid.But its printing Invalid.

Could someone help me to get it resolved?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
udaya chandran
  • 127
  • 1
  • 2
  • 7

3 Answers3

6

Use strcmp to compare strings instead of comparing pointers. Pointers are equal only when they point to the same address.

Anton Malyshev
  • 8,686
  • 2
  • 27
  • 45
2

You are comparing the value of two pointers in

if(str2[k] != str1[j])

which, in your program is never false because they have different values, they are pointing to different strings (which happen to have the same value at times).

It seems that you want to compare the strings by their content, to do that you would use strcmp as in:

if(strcmp(str2[k], str1[j]))

Selçuk Cihan
  • 1,979
  • 2
  • 17
  • 30
2

In this if statement

if(str2[k] != str1[j])

there are compared addresses of tokens (of their first characters) in argv[1] with addresses of string literals in the array str1. It is evident that their addresses are different because they are located in different places of memory.

Instead of comparing addresses you have to compare the tokens themselves as strings. To perform this task use standard C function strcmp declared in header <string.h>. For example

if( strcmp( str2[k], str1[j] ) != 0 )
//...

Take into account that the initialization of the token token with a string literal

char *token = "";

does not make sense. You could declare the pointer just like

char *token;

or like

char *token = NULL;

Also the command line parameter can contain less than two tokens. In this case your program will have undefined behavior.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335