So I commented out the scanf part (and just initialized it with my own string), why does it crash if I use scanf? I believe the actual arguments I’ve put in scanf(); are correct.
#include <stdio.h>
#include <stdlib.h>
int strendmilan(char *s,char *t)
{
int scntr = 0,tcntr = 0;
while(*(s+(scntr++)) != '\0')
;
--scntr;
while(*(t+(tcntr++)) != '\0')
;
--tcntr;
while(tcntr >= 0)
if(*(s+scntr--) == *(t+tcntr--))
;
else
return 0;
return 1;
}
int main()
{
char *s,*t;
/*
scanf("%s",s);
scanf("%s",t);
*/
s = "HAHAHACOOL";
t = "COOL";
if(strendmilan(s,t) == 0)
printf("NOT");
else
printf("YES");
getch();
}
The problem is that you are sending
scanf()a pointer to a buffer (e.g.,s) where it can store the information read, but you haven’t allocated any buffer space pointed to bys(and same fort)So you can either allocate an array of char for
s, or usemalloc()to allocate some storage and assign the return value of that call tos.