Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

182
Views
How to compare pointer and integer?

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?

9 months ago · Santiago Trujillo
2 answers
Answer question

0

"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.

9 months ago · Santiago Trujillo Report

0

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;

}
9 months ago · Santiago Trujillo Report
Answer question
Find remote jobs