-
array problem
I found it useless to do something on array such as if(myArray=[1,2,3]), and the only way I figure out is to compare the elements in each array separately just like this:
for(i=0; i<3; i++)
{
if(myArray[i]==myVar)
{
doThis();
}
}
is there any other way conveniently ?:rolleyes:
-
I dont think so
Unfortunately, I dont think you can do that for integer arrays.
However, you can compare char arrays using
strcmp(array1, array2)
This is how I think you can compare integer arrays
PHP Code:
int flag = 0;
int myArray = {1,2,3}
int myVar = {1,2,3};
for(i=0; i<3; i++)
{
if(myArray[i]==myVar[i])
{
flag++;
}
}
so if flag = 3, it means all elements are the same.
-
sorry, forgot to mention myVar was a variable itself.:o
and I just want figure out how could we do something like this:
if(myArray==[1,2,3]).
thanx in advance~
-
not possible
to the best of my knowledge, i dont think you can do that directly.
why dont you try making a function yourself... just like strcmp
call it
intarraycmp(int *array1, int *array2)
it should return 0 if there are no differences, and 1 if there's
atleast one difference.
Use the same logic as I did in my previous post.
-
Moonwalker is correct because 'myArray' acts as a pointer to the first location (zero-position) of the array.
Without incrementing the pointer, as with the FOR loop that was shown, it will simply sit at the first array position. Good for comparing that value, but not very handy for comparing the remainder of the array.
The function idea is a good one. :)
-Skipper
-
thanx guys~ I'll do better with array. ;)
-
array comparison
If you have two array pointers to int arrays, and both arrays have a fixed size which is known, why not just do this:
BOOL CompareIntArrays(int *ipArray1, int *ipArray2, int iArraySize)
{
return (memcmp(ipArray1, ipArray2, iArraySize * sizeof(int)) == 0);
)