Traversing an array

This is a discussion on Traversing an array within the C Programming forums, part of the General Programming Boards category; I have what I think is a simple question. I have an array as follows: char array[5][100] = { "string ...

  1. #1
    Someone give me a job DigitalKhaos's Avatar
    Join Date
    Apr 2003
    Posts
    4

    Traversing an array

    I have what I think is a simple question.

    I have an array as follows:

    char array[5][100] = {
    "string 1",
    "string 2",
    "string 3",
    "string 4",
    "string 5"
    };

    then I attempt to traverse the array to find a matching string as follows:

    int x;

    for(x = 0; x < sizeof(array); x++){
    if(strncmp(my_buffer, array[x], strlen(array[x])
    /* do some stuff */

    Do I have this correct, will this work, is the "sizeof" the array what I'm looking for here?

    Any info is greatly appreciated.

    Thanks All!!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >Do I have this correct, will this work, is the "sizeof" the array what I'm looking for here?

    I think you want the number of elements, not the entire array size.
    Code:
       for ( x = 0; x < sizeof(array)/sizeof(*array); x++ )
       {
          if ( strncmp(my_buffer, array[x], strlen(array[x]) ) == 0 )
          {
             /* ... */
          }
       }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>traverse the array to find a matching string
    Using this code:
    >>if ( strncmp(my_buffer, array[x], strlen(array[x]) ) == 0 )
    won't get you a matching string. For example, if my_buffer contained "string 21", you'd get a match on "string 2", which I guess would be wrong (or not, depending on your design requirements).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Someone give me a job DigitalKhaos's Avatar
    Join Date
    Apr 2003
    Posts
    4
    Quote Originally Posted by Hammer
    >>traverse the array to find a matching string
    Using this code:
    >>if ( strncmp(my_buffer, array[x], strlen(array[x]) ) == 0 )
    won't get you a matching string. For example, if my_buffer contained "string 21", you'd get a match on "string 2", which I guess would be wrong (or not, depending on your design requirements).
    I guess the way around that would be:

    Code:
    if(strncmp(my_buffer, array[x], strlen(my_buffer)) == 0)
    BTW - I've decided to trash this method, and instead use a list

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21