3

I am working on a C project. I am new to C so forgive me if this is a simple question.

I have a char ** which is an array that contains various values. Out of all the values I have no problem what the values contain except for one where it keeps on core dumping on strcmp.

Below is my code:

if (strcmp(reportParameterArray[P_UNIQUECOLS],'Y') != 0)
{
    //Do something
}

P_UNIQUECOLS is an enum which is the index of where to retrieve the value from. If I look at the value in GDB I can see that it either contains Y or N as it should but for some reason the app is crashing.

Thanks for any help you can provide.

3 Answers 3

11

Use "Y" which is a string literal, not 'Y' which is a char literal, as strcmp() takes two const char* arguments.

Compile with warnings at the high level and don't ignore them (preferably treat them as errors).

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

2 Comments

And pay attention to the compiler warnings, or get a better compiler!
Thanks this worked. There weren't any compiler warnings or errors. This is a really old server I am having to build on so the compiler isn't great
2

'Y' is a single character, strcmp expects a pointer. If your compiler is not giving you at least a warning, you're not using it with the right options.

Use "Y" instead of 'Y'.

Comments

2

Yes, strcmp takes both parameters as string.

If the application is crashing, and getting segmentation fault that means you are accessing some unauthorized memory location. Check whether "reportParameterArray" you have allocated memory or not.

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.