1

I'm trying to write a function in C that acts like a command prompt. I'm getting a segfault error. My logic of this follows as: getchar() and check if its a space or not. If it's not a space, add the char onto the msg. If it is a space, end the word and add it onto the command array, and get next word. When it gets new line, it ends.

void getCommand(char *command[10]){
char *msg = "";  //Creates ptr
char c;
int length = 0;
while(getchar() != '\n'){   //while char != new line
    c = getchar();      // get char
    if(c == ' '){        //if char equal space
        c = '\0';    //make c equal end of line
        msg[length] = c;    //add c to the end of line
        strcpy(*command, msg);  //copy the word into the first part of the array
        command++; //increase command array
    }
    else{                           //if word does not equal space
        msg[length] = c;       //add char to the msg
        length++;             //increase length by one
    }
}

}

  • You've allocated no space for your msg string. – goji Oct 01 '13 at 03:08
  • It's been a while since I've used C. `char* msg = (char *) malloc (sizeof(char) * 30);` ? – confusedProgrammer Oct 01 '13 at 03:14
  • Alright, check. Allocated enough space. Still getting a segFault – confusedProgrammer Oct 01 '13 at 03:18
  • @user2808307 `malloc returns void pointer`. that can be assigned to any pointer. so you need not to cast.see [Do I cast the result of malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) and sizeof(char) is 1. you can avoid multiplying with 1. – Gangadhar Oct 01 '13 at 03:33

1 Answers1

0

You have not Allocated memory for msg pointer and you need make length value Zero when space occurs

char *msg = ""; //declare static array or allocate some space As below
char msg[MAX_SIZE+1];  or char *msg=malloc(MAX_SIZE+1);  

Modify your code

   i=length=0;   

   while(((c = getchar())!='\n') && (i != 10)){       //read here and check here it self  

        if(c == ' '){
            msg[length] = '\0';        //instead of storing null in char and then in string directly store null in string
            strcpy(command[i++], msg); //use commands pointer array   
            length=0;                  //you should make this zero when space occurs
                   }  

        else                        
            msg[length++] = c;         //if not space copy into string

       }       

    commands[i]=NULL;                  //this tells you how many words you have in the given line.       

Also check with commands pointer array memory allocation. each pointer must have enough space to store msg string.

Gangadhar
  • 10,248
  • 3
  • 31
  • 50
  • 1
    Make `c` an `int` and try `while (((c = getchar()) != '\n') && (c != EOF) && (i < 10) {` to check for EOF, and because the last valid index for `command` is 9 not 10.. – Dmitri Oct 01 '13 at 03:43
  • making `c an int is indeed because of getchar() returns int`. and checkinh for EOF is also Nice idea. `i!=10 is same as i<10` when i becomes 10 at that moment loop fails. – Gangadhar Oct 01 '13 at 03:50
  • 1
    Oops... I should've noticed that (`i < 10` vs. `i != 10`)... yes, either is fine. – Dmitri Oct 01 '13 at 03:56