I'm having some trouble in my program after using strtok()
. I have tested printf
and the opt
is always getting what it is supposed to. But when i reach the if clauses, nothing runs. For example, when i type a banana
it should run the function a1, but the program just ends.
#include <stdio.h>
#include <string.h>
char pedido[80],*opt
int main(){
fgets(request,80,stdin);
opt=strtok(request," ");
if (opt=="a"){a1();}
if (opt=="q"){a2();}
if (opt=="N"){a3();}
if (opt=="A"){a4();}
if (opt=="r"){a5();}
if (opt=="R"){a6();}
if (opt=="C"){a7();}
if (opt=="p"){a8();}
if (opt=="E"){a9();}
if (opt=="m"){a10();}
if (opt=="l"){a11();}
if (opt=="L"){a12();}
return 0;
}
Statements like this
if (opt=="a"){a1();}
do not make sense because the opt can never be equal to "a". The value of opt is either NULL or the address of a character in the array request. As the string literal and the array occupy different extents of memory then their addresses are not equal.
You should use the standard C function strcmp as for example
if ( strcmp( opt, "a" ) == 0 ){a1();}