0

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.

2
Contributors
2
Replies
3
Views
12 Years
Discussion Span
Last Post by idr1978
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.