3

I'm trying to check if pointer is pointing at some char.

Like this:

#include<stdio.h>
#include<string.h>
#define a 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 I constantly get warning: warning: comparison between pointer and integer if(*s=="a")

Is it possible to compare pointer and integers?

Is there another way to resolve this problem?

Can I find out at which letter is pointing *s without printing it?

2 Answers2

11

"a" is not a character, it is a string literal. 'a' is a character literal, which is what you are looking for here.

Also note that in your comparison *s == "a" it is actually the "a" which is the pointer, and *s which is the integer... The * dereferences s, which results in the char (an integer) stored at the address pointed to by s. The string literal, however, acts as a pointer to the first character of the string "a".

Furthermore, if you fix the comparison by changing it to *s == 'a', you are only checking whether the first character of s is 'a'. If you wish to compare strings, see strcmp.

Arkku
  • 41,011
  • 10
  • 62
  • 84
1

chars are enclosed in '' not ""

#include<stdio.h>
#include<string.h>
#define a 3
int func(char *);
int main()
{
char value = 'a';
char *s=&value;
int type;
type=func(s);
printf("%d",type);

    return 0;
}
int func(char *s)
{
int type;
if(*s=='a') //or if(*s==3)
{
type=1;
}
return type;

}
user3476093
  • 704
  • 2
  • 6
  • 11