1

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...

3
  • What's the value of MAX_INPUT, what are you passing to the function when calling it? It's hard to help you if your question is so vague. Commented Oct 6, 2013 at 0:22
  • Sorry, it's #define MAX_INPUT 64 Commented Oct 6, 2013 at 0:23
  • 2
    You need to use a string literal, not a character constant, not sure how it even compiles. Commented Oct 6, 2013 at 0:23

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I will make this correct answer when the time runs out ... I would have never noticed that ... even though I should of..
-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.