Thread: Segmentation fault

  1. #1
    Registered User
    Join Date
    Oct 2004

    Segmentation fault

    Hi, I have a problem with the segmentation fault.
    This method adds letters to a string(name), but I dont know if i'm doing the wright way, I suposse itīs wrong, because the output is not the same of the line of the file and in the end of the output it appears : segmentation fault.
    I want another way to add the letters to the string.

      int ch;
      int add = 0;
      char *name;
      FILE *file = fopen(".tabpasswd", "r");
      while((ch=fgetc(file)) != EOF) {
        if(ch != ':') {
          name[add] = ch;
       printf("%s", name);


  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    You don't allocate any memory for name to which you can write to, it's merely a dangling pointer. You also fail to null terminate the string, had you actually had memory to which you could write, so that you could print a valid C-string.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    You have no space allocated for 'name' when you do this:

    char *name;
    All you have done there is to declare a pointer variable. There are a couple of different ways you could allocate some space which belongs to you, and you can manipulate. Try declaring 'name' as an array, something like this:

             ARR_MAX = 25   /* or whatever arbitrary size you need your array to be */
    char name[ARR_MAX];
    Alternatively, you could malloc some space and then you would be able to user your pointer variable '*name'..

  4. #4
    Registered User
    Join Date
    Oct 2004
    But i dont want to give a size to the array.
    I want to use it like the ArraList in java.
    How can i do this?


  5. #5
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    >>But i dont want to give a size to the array.
    You have to give the size either you use array or dynamic array.
    If you are afraid of giving size.
    A really stupid method would be give a initial size 1 and reallocate memory(previous +1) everytime in your if loop.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 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