Why do I get a segfault after I input the value of s?Code:1 #include <stdio.h> 2 3 main() 4 { 5 char *x; 6 char *s; 7 scanf("%s", s); 8 x = "2352"; 9 printf("s: %s\n", s); 10 printf("x: %s\n", x); 11 }
Your help is appreciated.
Why do I get a segfault after I input the value of s?Code:1 #include <stdio.h> 2 3 main() 4 { 5 char *x; 6 char *s; 7 scanf("%s", s); 8 x = "2352"; 9 printf("s: %s\n", s); 10 printf("x: %s\n", x); 11 }
Your help is appreciated.
*s is a new pointer, at the start. It's pointing to NULL, most likely, depending on your compiler.
Now you're assigning a char, to NULL - that's the infamous "NULL pointer assignment".
You can't do that.
Turbo C doesn't crash with your code, but can't print out the char, either, just gives the error. If I kept doing it, it would crash, of course.
So how would I go about doing what my code intends to do?
Give it something to point to, like you did with x.
There might be one more problem, but try that.