Segmentation fault

This is a discussion on Segmentation fault within the C Programming forums, part of the General Programming Boards category; Hello, I need a little help. This code works without any errors. Code: int main() { int i=0; char c=0; ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    Segmentation fault

    Hello,
    I need a little help.
    This code works without any errors.
    Code:
    int main()
    {
    	int i=0;
            char c=0;
    	char *command;
    
    	while ((c=getchar()) != '\n'){  
    	    command[i]=c;
    	    i++;
    	 }
    
    	command[i]='\0';
    	return 1;
    }
    
    However, when I add a new variable, for example:
    
    int main()
    {
    	int i=0, k=0;
            char c=0;
    	char *command;
    
    	while ((c=getchar()) != '\n'){  
    	    command[i]=c;
    	    i++;
    	 }
    
    	command[i]='\0';
    	printf(" k is %d\n", k+3);[/B]
    	return 1;
    }
    the codes stop working and I get a "Segmentation fault" announcement.
    What is the problem?

    Thanks
    Last edited by Salem; 04-27-2010 at 10:52 AM. Reason: [code][/code] tags - learn to use them yourself!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    Well, in both cases, command is a pointer to char that does not point to anything in particular, yet you write command[i] = c;

    Incidentally, c should be an int instead of a char since getchar() returns an int (as is necessary to compare with EOF, should you choose to do so).

    Oh, and please post code in [code][/code] bbcode tags.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    I've change c type to int, but I still got segmentation fault (and I'v change to compare with EOF)

    And moreover, it stopped working without the new variable.
    Last edited by ksusha; 04-27-2010 at 10:52 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    Yeah, but that does not address the first part: command does not point to anything. Change it to be an array instead, then check that i is always a valid index before you do command[i].
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > char *command;
    Where does this point?

    You've written
    char *command = somerandomaddressinmemory;

    Rearrange the code, you're walking in a different part of the mine-field.
    The failure to blow up doesn't make it any less of a minefield.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    The problem is that my input is of unknown length, and that's why I can't use an array

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    Quote Originally Posted by ksusha
    The problem is that my input is of unknown length, and that's why I can't use an array
    In that case, use malloc(), realloc() and free(). You would keep track of both the current size of the dynamic array (as in the number of elements in use) and its capacity. The former is already done with i, so you need another variable for the latter. When the size is about to exceed capacity, you "expand" the dynamic array with realloc().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Ok, I'll try.
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 03:01 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06: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, 01:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21