c - How to compare pointer and integer? -
this question has answer here:
- how compare strings? 6 answers
i'm trying check if pointer pointing @ char.
like this:
#include<stdio.h> #include<string.h> #define 3 int func(char *); int main() { char *s="a"; int type; type=func(s); printf("%d",type); return 0; } int func(char *s) { int type; if(*s=="a") { type=1; } return type; } but warning: warning: comparison between pointer , integer if(*s=="a")
is possible compare pointer , integers?
is there way resolve problem?
can find out @ letter pointing *s without printing it?
"a" not character, string literal. 'a' character literal, looking here.
also note in comparison *s == "a" "a" pointer, , *s integer... * dereferences s, results in char (an integer) stored @ address pointed s. string literal, however, acts pointer first character of string "a".
furthermore, if fix comparison changing *s == 'a', checking whether first character of s 'a'. if wish compare strings, see strcmp.
Comments
Post a Comment