scanf segfaults and various other anomalies inside while loop

Posted by Shadow on Stack Overflow See other posts from Stack Overflow or by Shadow
Published on 2010-04-12T22:16:32Z Indexed on 2010/04/12 22:23 UTC
Read the original article Hit count: 266

Filed under:
|
|
while(1){
    //Command prompt
    char *command;
    printf("%s>",current_working_directory);
    scanf("%s",command);<--seg faults after input has been received.
    printf("\ncommand:%s\n",command);
}

I am getting a few different errors and they don't really seem reproducible(except for the segfault at this point >.<). This code worked fine about 10 minutes ago, then it infinite looped the printf command and now it seg faults on the line mentioned above. The only thing I changed was scanf(">%s",command); to what it currently is. If I change the command variable to be an array it works, obviously this is because the storage is set aside for it.

1) I got prosecuted about telling someone that they needed to malloc a pointer* (But that usually seems to solve the problem such as making it an array) 2) the command I am entering is "magic" 5 characters so there shouldn't be any crazy stack overflow. 3) I am running on mac OSX 10.6 with newest version of xCode(non-OS4) and standard gcc 4) this is how I compile the program: gcc --std=c99 -W sfs.c

Just trying to figure out what is going on. Being this is for a school project I am never going to have to see again, I will just code some noob work around that would make my boss cry :) But for afterwards I would love to figure out why this is happening and not just make some fix for it, and if there is some fix for it why that fix works.

© Stack Overflow or respective owner

Related posts about c

    Related posts about scanf