c - compare 2 inputs from keyboard with strcmp leads to segmentation fault -
i new student in c language , come this. code:
#include <stdio.h> #include <string.h> int main(void) { char str[80]; printf("enter sth: "); char st1 = gets(str); printf("enter sth: "); char st2 = gets(str); if(strcpy(st1,st2)) printf("same\n"); else printf("different\n"); return 0; }
my goal check if 2 strings enter keyboard same. compile , warnings:
hello.c: in function ‘main’: hello.c:9:16: warning: initialization makes integer pointer without cast [enabled default]
hello.c:12:16: warning: initialization makes integer pointer without cast [enabled default]
hello.c:14:5: warning: passing argument 1 of ‘strcpy’ makes pointer integer without cast [enabled default]
/usr/include/string.h:128:14: note: expected ‘char * restrict’ argument of type ‘char’
hello.c:14:5: warning: passing argument 2 of ‘strcpy’ makes pointer integer without cast [enabled default]
/usr/include/string.h:128:14: note: expected ‘const char * restrict’ argument of type ‘char’
enter sth: asd
enter sth: asd
output: segmentation fault (core dumped)
segmentation fault saw error when want access sth doesnt exist!
i search little here in stackoverflow similar questions , here dont understand why code isnt working. thank you!
you treating address of char
variable string , using strcpy
instead of strcmp
. this:
char st1 = gets(str); char st2 = gets(str); if(strcpy(st1,st2))
was meant be:
char st1[255], st2[255]; scanf("%254s", st1); scanf("%254s", st2); if(strcmp(st1, st2) == 0)
Comments
Post a Comment