No, not necessary. An if does not need an else.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; } }
Now my program crashes (The test program for just this function).
This Is my function now:
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.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; }
Nothing to see here, move along...
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...
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...
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.
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).
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.
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:
But now I'm getting al kinds of warnings again, and I really don't know what I'm doing anymore.Code:const char comb_array[][1000] = { "13", "26", "158", "86", "88", "1212", "142", "89", "14", "410", "1116", "168", "12", "34" };
Nothing to see here, move along...
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.
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; }
"string" should preferably be of type const char*.
http://apps.sourceforge.net/mediawik..._be_const_char
It's all good info, but don't we need the truck to go in *front* of the trailer.
First things, first.
I know.
The OP is just overloaded, atm, and confused.