| | |
strcmp of 2-D array
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2004
Posts: 5
Reputation:
Solved Threads: 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.
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.
>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:
Alternatively you could use strncmp to compare a subset of the string, then use the address-of operator on the indexed array:
But that's kind of stupid unless the string subset is either variable or large enough for a direct comparison to be impractical.
Not if Training[0][0] is a character. strcmp compares strings, and a character is not a string. Use == instead:
C Syntax (Toggle Plain Text)
if ( Training[0][0] == '1' )
C Syntax (Toggle Plain Text)
if ( strncmp ( &Training[0][0], "1", 1 ) == 0 )
New members chased away this month: 4
![]() |
Similar Threads
- Question Reading from a file and inputting into a string array I am getting a errors. (C++)
- help me fix my program(strtok,strcmp,array) (C)
- Multidimensional array sort problem (C++)
Other Threads in the C Forum
- Previous Thread: Pointers (Part II)
- Next Thread: C function called interp?
Views: 2093 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory drawing dynamic executable feet fflush fgets file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide infiniteloop initialization interest intmain() kilometer lazy license linked linkedlist linux linuxsegmentationfault list match matrix meter microsoft multi mysql oddnumber odf open openwebfoundation pattern pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scheduling segmentationfault send shape single socketprogramming spoonfeeding stack standard strchr string strings structures student suggestions system test testautomation unix urboc user voidmain() win32 win32api windows.h






