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;
}
}
Printable View
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;
}
I'm sorry I should have refreshed my browser, I missed Elysia's answer. Let me see if i can fix it with that.
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;
}
What compiler are you using?
--
Mats
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"
};
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.
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.
I know.
The OP is just overloaded, atm, and confused.