Reputation: 1179
The following code is giving me a Segmentation fault
void parseInput(char usrIn[])
{
char retCmd[MAX_INPUT];
retCmd[0] = usrIn[0];
printf('Debug: %c\n', retCmd[0]);
}
This is my first big project in C, but I think it's the printf giving me fault .. however I'm not sure...
Upvotes: 1
Views: 247
Reputation: 1200
You need to make sure that the array is not zero-length. If it is, then the first element is empty and you will get a segfault when you try to access a non-existent element in the array. You can get the array length using sizeof(array) / sizeof(array[0])
, or by using int main(int argc, char** argv)
, and checking argc for the number of elements in the argv array.
Upvotes: -1
Reputation: 64682
Your original line:
printf('Debug: %c\n', retCmd[0]);
printf("Debug: %c\n", retCmd[0]);
Upvotes: 5