strcmp of 2-D array

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2004
Posts: 5
Reputation: idr1978 is an unknown quantity at this point 
Solved Threads: 0
idr1978 idr1978 is offline Offline
Newbie Poster

strcmp of 2-D array

 
0
  #1
Nov 9th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: strcmp of 2-D array

 
0
  #2
Nov 9th, 2004
>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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 5
Reputation: idr1978 is an unknown quantity at this point 
Solved Threads: 0
idr1978 idr1978 is offline Offline
Newbie Poster

Re: strcmp of 2-D array

 
0
  #3
Nov 9th, 2004
Thank you sir. Worked great!!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2093 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC