Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

176
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.