Can't pass pointers to scanf(“%s”,ptr);

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();
}

Answers


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 by s (and same for t)

char *s,*t; 
scanf("%s",s);

So you can either allocate an array of char for s, or use malloc() to allocate some storage and assign the return value of that call to s.


Need Your Help

how can i open a link without using “http” info..?

javascript jquery html html5 hyperlink

In my app, I have a field to fill by my user for their URL. After they fill I convert the url into a href link. But sometimes user does not add a http:// prefix to their link, so a link is not work...