[Solved] Why is this If statement not executing the code? [duplicate]


You did char * response. This makes a pointer variable to a character. Right now it is not pointing to any memory(it is some garbage value). scanf stores user input in consecutive memory addresses starting from the one pointed by response. as response is uninitialised, the input may not necessarily be stored on the stack(Don’t want that).

Now when you doresponse=="Dice" it doesn’t mean anything at all.


Some pretty basic stuff on arrays and pointers and their comparison.

int arr[10];

now arr points to the first member of the array, arr+1 points to second, arr+2 to third and so on. arr[i] is a shorthand way of saying *(arr+i).


String is also an array of characters.

char *str1="Hello";
char *str2="Hello";
if(str1==str2){...}

What the if statement in line three here does is, it compares to pointers, ie it checks whether they both point to the same location. Since this is not true, execution won’t go into the if block. What you want to do is compare the strings character by character. There is an inbuilt function in string.h called strcmp() which does that.

7

solved Why is this If statement not executing the code? [duplicate]