Boardy
Boardy

Reputation: 36217

Compare value in char** array

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.

Upvotes: 3

Views: 148

Answers (3)

Vasant
Vasant

Reputation: 21

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.

Upvotes: 2

Medinoc
Medinoc

Reputation: 6608

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

Upvotes: 2

hmjd
hmjd

Reputation: 122001

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

Upvotes: 11

Related Questions