Metie Metie - 18 days ago 5
C Question

Beginner program crashes (warnings: makes int from prt, expected const char* but got char)

I've just begun to learn c (and generally coding) and I don't understand the warning messages. Thank you for your help.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct record{
char name;
} record;

void findname(char name, record lib){
int h=0;
if (strstr(lib.name,name)!=NULL)
{
printf("whole name:%c",lib.name);
h=1;
}
if (h=0) printf("no result");
}

int main(){
char s;
record lib={
{ "bruce willis" }
};
scanf("%c",&s);
findname(s,lib);
return 0;
}

Answer

you have following issues :-

strstr need const char * in both args.
if(h=0) is wrong, for condition you need to check (h==0)
char name is too short to hold string.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct record{
  char name[10];
} record;

void findname(char *name, record lib){
int h=0;
if (strstr(lib.name,name)!=NULL)
{
    printf("whole name:%s",lib.name);
            h=1;
}
if (h==0) printf("no result");
}

int main(){
char s[10];

    record lib={
         .name = "sumit"
    };

          scanf("%s",s);
        findname(s,lib);
    return 0;
}