Thread: Wierd pointer problem

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    5

    Wierd pointer problem

    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 }
    Why do I get a segfault after I input the value of s?

    Your help is appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    *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.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    So how would I go about doing what my code intends to do?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Give it something to point to, like you did with x.

    There might be one more problem, but try that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-08-2009, 11:47 AM
  2. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  3. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 12:42 PM
  4. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM