Quick question:

I have a 2-D array called Training[693][19] of characters 0, 1 or 2 (yes characters, not integers). I'm trying to run an IF statement similar to this:

if (strcmp(Training[0][0], "1")==0)
printf ("Successful case \n ");

BUT, apparently the IF statement is wrong. I'm trying to determine if the first element in the Training array is "1". What am I doing wrong? I get a core dump whenever I run it. Can't I use "strcmp" like this? Please help!! I've been working on this for the last 2 days.

Recommended Answers

All 2 Replies

>Can't I use "strcmp" like this?
Not if Training[0][0] is a character. strcmp compares strings, and a character is not a string. Use == instead:

if ( Training[0][0] == '1' )

Alternatively you could use strncmp to compare a subset of the string, then use the address-of operator on the indexed array:

if ( strncmp ( &Training[0][0], "1", 1 ) == 0 )

But that's kind of stupid unless the string subset is either variable or large enough for a direct comparison to be impractical.

Thank you sir. Worked great!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.