Reputation: 1713
I want to check an array in a condition. Let's take this simple code below:
#include <stdio.h>
int main()
{
int array[] = {1,2,3,4,5}; // initializing an array
if(array[] == {1,2,3,4,5}) // using as condition
{
printf("worked");
}
else printf("not worked");
return 0;
}
But it gives an error:
In function 'main':|
C:\Python32\Untitled4.c|5|error: expected expression before ']' token|
||=== Build finished: 1 errors, 0 warnings ===|
So how should I use an array in a condition?
Upvotes: 4
Views: 15697
Reputation: 70422
Another different example of "you can assign with =
, but not compare with ==
is with struct
types.
struct S {
int array[5];
};
struct S a = { { 1, 2, 3, 4, 5 } };
struct S b;
b = a; /* assignment ok */
if (b == a) { /* equality not ok */ }
So how should I use an array in a condition?
You can use memcmp
.
if (memcmp(b.array, a.array, sizeof(b.array)) == 0) { /* ... */ }
Edit: Note that memcmp
performs a byte by byte comparison of two memory locations. As Eric points out, if your object has padding bits with different values, or if your type has multiple bit representations for the same value, then memcmp
may produce false negatives.
This issue is avoided in practice by initializing objects in a normalized way (e.g., using memset
to 0
out the memory before doing member assignments). Edit: Even so, the comparison via memcmp
may still produce a false negative. The maximally portable way to perform the comparison is by comparing each corresponding member with each other. (Follow the link for the gory details.)
You can create a helper function to make this easier to do:
/* API as memcmp, but memory treated like array of int, remainder bytes ignored */
int memcmp_int (const void *a, const void *b, size_t sz) {
const int *aa = a;
const int *bb = b;
while (sz >= sizeof(int)) {
if (*aa < *bb) return -1;
if (*aa > *bb) return 1;
++aa;
++bb;
sz -= sizeof(int);
}
return 0;
}
Then, the code can be modified very simply:
if (memcmp_int(b.array, a.array, sizeof(b.array)) == 0) { /* ... */ }
Upvotes: 4
Reputation: 78923
If you have a modern C compiler, at least C99, you can use a compound literal and a function to do that comparison:
if(memcmp(array, (int[]){1,2,3,4,5}, sizeof array) == 0) {
printf("worked");
}
memcmp
(memory compare) compares the data to which the two
pointers point.(int[]){1,2,3,4,5}
is the compound literal, something with the type in ()
and then an initializer in { }
as you have in the declaration of your variable.Edit: As Eric correctly remarks, memcmp
is only a valid comparison if the base type of your array (here this is int
) has no padding bits or bytes. For int
this is uncommon these days, so what I describe is fine on usual platforms. If you'd have other, more complex, data types some day, you would have to write your own comparison function for arrays of that type.
Upvotes: 6
Reputation: 59617
This format:
int array[]={1,2,3,4,5};
Can only be used in the initialization or arrays or structs. This is referred to as aggregate initialization. This can't be used to generally represent an array.
Furthermore, you can't use ==
to compare arrays or structs in C.
To code your condition, you'll need to check each value.
Upvotes: 5
Reputation: 5791
You need to loop through the array. A simple loop would look like this:
#include <stdio.h>
int main()
{
int array[] = {1,2,3,4,5}; //initializing an array
int iter = 0;
for (; iter < sizeof(*array); iter++){
printf("Iteration: %i\n", iter);
// TOOD: Add your condition here
}
return 0;
}
Upvotes: 3