944,142 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2640
  • C RSS
Nov 9th, 2004
0

strcmp of 2-D array

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
idr1978 is offline Offline
5 posts
since Nov 2004
Nov 9th, 2004
0

Re: strcmp of 2-D array

>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:
  1. 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:
  1. 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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 9th, 2004
0

Re: strcmp of 2-D array

Thank you sir. Worked great!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
idr1978 is offline Offline
5 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Pointers (Part II)
Next Thread in C Forum Timeline: C function called interp?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC