Peter Chappy
Peter Chappy

Reputation: 1179

Segmentation Fault printf C

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

Answers (2)

axon
axon

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

abelenky
abelenky

Reputation: 64682

Your original line:

printf('Debug: %c\n', retCmd[0]);


How it should be:

printf("Debug: %c\n", retCmd[0]);


Notice the change from single-quotes to double-quotes

Upvotes: 5

Related Questions