Thread: Segmentation Fault

  1. #1
    Registered User
    Join Date
    Jul 2005

    Segmentation Fault

    I am getting a core dump when I try scanning in the values. Please help.
    #include <stdio.h>
    #include <string.h>
    int main()
            char *strings[50];
            int amnt;
            printf("Enter the number of strings (0 to 50)\n> ");
            printf("Enter %d strings on separate lines\n",amnt);
            int i =0;
    for (i=0;i<amnt;i++)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    For starters, you never allocated any memory for the strings.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Just because you have a pointer somewhere, doesn't mean you own the memory it is pointing to. In this case, you have 50 pointers that don't point anywhere, but you try to use them anyway. You will need memory for each of those pointers (either malloc in a loop, or a 2D character array seem like your options here).

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    What you will need to do is allocate space for your pointer, say malloc for 25 chars, right before your scanf assignment.

    To check that your strings were inserted, you can create another 'for loop' to cycle through your array of strings.

    Then you will need another for loop to free those malloc calls, or you will cause a memory leak further on down the road......lots of fun.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM