Olof
Olof

Reputation: 797

C: strcmp error

I have two strings to compare to check if they are the same and it worked but when I opened the project today it game me the errors.

warning C4047: 'function' : 'const char *' differs in levels of indirection from 'char (*)[5]'
warning C4024: 'strcmp' : different types for formal and actual parameter 1
warning C4047: 'function' : 'const char *' differs in levels of indirection from 'char (*)[5]'
warning C4024: 'strcmp' : different types for formal and actual parameter 1

printf("Your answer: ");
scanf_s("%s", &answer, 5);
//strlwr(answer);
_strlwr(answer);
if ((strcmp(&answer, "ja") && location[question].rightAnswer == 1) || (strcmp(&answer, "nej") && location[question].rightAnswer == 0) || (strcmp(&answer, "yes") && location[question].rightAnswer == 1) || (strcmp(&answer, "no") && location[question].rightAnswer == 0)){
    printf("Right answer!\n\n");
    points += difficulty;
}
else if ((strcmp(&answer, "ja") && location[question].rightAnswer != 1) || (strcmp(&answer, "nej") && location[question].rightAnswer != 0)){
    printf("Wrong answer!\n\n");
}
else{
    printf("Don't understand your input\n");
}

Upvotes: 0

Views: 5318

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

The warnings are clear enough. The type of the first argument of strcmp does not correspond to the type of the parameter.

The type of the parameter is const char * while you are trying to pass a pointer to an array of five characters char (*)[5]

You have to write simply

strcmp( answer, "ja" ) 

In this case character array answer that has type char[5] is converted to pointer to its first element that is required by the function.

Take into account that if you want to check the equality of strings you should write

if ( strcmp( answer, "ja" ) == 0 ) /*...(/

So it seems that the correct record of the first if statement will look like

if ( ( strcmp( answer, "ja" )  == 0 && location[question].rightAnswer == 1 ) || 
     ( strcmp( answer, "nej" ) == 0 && location[question].rightAnswer == 0 ) || 
     ( strcmp( answer, "yes" ) == 0 && location[question].rightAnswer == 1 ) || 
     ( strcmp( answer, "no"  ) == 0 && location[question].rightAnswer == 0 ) )
{
    printf("Right answer!\n\n");
    points += difficulty;
}

The same way also the next else-if statement has to be rewritten .

It seems also that statement

scanf_s("%s", &answer, 5);

has to be written like

scanf_s("%s", answer, sizeof( answer) );

Upvotes: 1

alpartis
alpartis

Reputation: 1122

It looks like you defined answer like this (which is fine):

char answer[5];

In that case, answer is already a "pointer to a string" for all intents and purposes. So that's all you need to use in your calls to the string utility functions:

strcmp(answer, "ja");

Upvotes: 1

legends2k
legends2k

Reputation: 32904

strcmp takes two const char* and for the second argument you're passing "ja" which is correct, while for the first argument, you're passing pointer to an array of 5 chars, which is the reason you see char (*) [5] in the compiler error. Fixing it would be to do

strcmp(answer, "ja");    // & operator NOT applied to answer

When doing this, array decay would kick-in and answer which is of type char [5] would become char* a char pointer pointing to the first element of the array.

When you apply the & operator, you'd get a pointer to the array itself and not the decay behaviour. This is one of the few places when decay would not happen. The other two being sizeof and string literal initializer. See here for details.

Upvotes: 3

Related Questions