Checking array for string

This is a discussion on Checking array for string within the C Programming forums, part of the General Programming Boards category; Code: for(i = 0; i < length_of_array; i++) { if (strcmp(string1_to_check_for, array_to_check[i]) == 0 || strcmp(string2_to_check_for, array_to_check[i]) == 0) { ...

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Code:
    for(i = 0; i < length_of_array; i++)
    {
        if (strcmp(string1_to_check_for, array_to_check[i]) == 0 ||
            strcmp(string2_to_check_for, array_to_check[i]) == 0)
        {
            break;
        }
    }
    No, not necessary. An if does not need an else.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    Now my program crashes (The test program for just this function).

    This Is my function now:
    Code:
    int stringcheck(const char* string1_to_check_for,const char* string2_to_check_for,const char* array_to_check[][1000],int length_of_array){
    
    int i=0;
    int result=0;
    
    for(i=0;i<length_of_array;i++){
             if( strcmp(string1_to_check_for,array_to_check[i][1000])==0 || strcmp(string2_to_check_for,array_to_check[i][1000])==0 ){
                                     result = 1;
                                     break;
             }
             
    }     
    
    return result;
    }
    It doesn't give any errors with compiling anymore, so then it probably has something to do with an overflow or maybe an infinite loop, but I don't see it.
    Nothing to see here, move along...

  3. #18
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    I'm sorry I should have refreshed my browser, I missed Elysia's answer. Let me see if i can fix it with that.
    Nothing to see here, move along...

  4. #19
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    Now I have this, and it still crashes:

    Code:
    for(i = 0; i < length_of_array; i++)
    {
        if (strcmp(string1_to_check_for, array_to_check[i][1000]) == 0 ||
            strcmp(string2_to_check_for, array_to_check[i][1000]) == 0)
        {
            return result = 1;
            break;
        }
    }   
    
    return result = 0;
    }
    Nothing to see here, move along...

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What compiler are you using?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to compare strings. array_to_check[i][1000] is not a string, it is a single character (or would be if it existed, which it doesn't).

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Actually, it's a pointer to const char, because the prototype is wrong.
    int stringcheck(const char* string1_to_check_for,const char* string2_to_check_for, const char* array_to_check[][1000],int length_of_array)
    As you see, it's a 2D array of pointers, which is wrong.
    It should be a 2D array of const char. And then tabstop's answer is correct and my original code that I showed would also be correct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    I'm using dev-cpp, is that a good choice? I use it because we used it at the university, so i figured it was probably good.

    Now I'm really confused, as usual with all this pointing and array stuff.

    So in my function i have a constant array of strings: char const array_to_check[][1000]
    And in the if argument i should have one of these strings: array_to_check[i][1000]?
    And in the main my array now looks like this:

    Code:
    const char comb_array[][1000] = 
    {    "13",
         "26",
         "158",
         "86",
         "88",
         "1212",
         "142",
         "89",
         "14",
         "410",
         "1116",
         "168",
         "12",
         "34"
    };
    But now I'm getting al kinds of warnings again, and I really don't know what I'm doing anymore.
    Nothing to see here, move along...

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So you wanted to pass an array of strings. comb_array is an array of strings. Pass it in and be done with it.

    Inside the function you want to compare strings. array_to_check[i], being one of your array of strings, is a string. Use it.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Quote Originally Posted by Ayreon View Post
    I'm using dev-cpp, is that a good choice? I use it because we used it at the university, so i figured it was probably good.

    Now I'm really confused, as usual with all this pointing and array stuff.

    So in my function i have a constant array of strings: char const array_to_check[][1000]
    And in the if argument i should have one of these strings: array_to_check[i][1000]?
    And in the main my array now looks like this:

    Code:
    const char comb_array[][1000] = 
    {    "13",
         "26",
         "158",
         "86",
         "88",
         "1212",
         "142",
         "89",
         "14",
         "410",
         "1116",
         "168",
         "12",
         "34"
    };
    But now I'm getting al kinds of warnings again, and I really don't know what I'm doing anymore.
    You accept whatever type you pass. Since the type is const char[][1000], then you must accept const char[][1000]. Simple.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Consider an array, a[][] of type char:

    Let's give it some of your digits, since you have some to spare, apparently.

    Code:
    //string searching through a 2D char array: strsrch.c
    #include <stdio.h>
    
    int main(void) {
    
       int i;
    
       char a[4][8] = { 
          { "1116" },
          { "168" },
          { "12" },
          { "34" }
       };
    
       char *string = "34";
    
       //now search the string array for "34"
    
       for(i = 0; i < 4; i++)  {
          if((strcmp(string, &a[i])) == 0)
             printf("\ntarget string \"%s\" is in line %d of array a[] \n", string, i);
    
       }
       i = getchar();
       return 0;
    }

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    "string" should preferably be of type const char*.
    http://apps.sourceforge.net/mediawik..._be_const_char
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's all good info, but don't we need the truck to go in *front* of the trailer.

    First things, first.

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    So if you would actually use it in the code in the first place, there would be no reminder.
    All I'm trying to do is make sure no one gives off bad coding practices to a newbie.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I know.

    The OP is just overloaded, atm, and confused.

Page 2 of 6 FirstFirst 123456 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 08:01 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 01:20 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Checking maximum values for dynamic array...
    By AssistMe in forum C Programming
    Replies: 1
    Last Post: 03-21-2005, 12:39 AM
  5. Replies: 5
    Last Post: 05-30-2003, 01:46 AM

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