Almost all languages have a foreach loop or something similar. Does C have one? Can you post some example code?
Eric's answer doesn't work when you're using "break" or "continue".
This can be fixed by rewriting the first line:
Original line (reformatted):
for (unsigned i = 0, __a = 1; i < B.size(); i++, __a = 1)
Fixed:
for (unsigned i = 0, __a = 1; __a && i < B.size(); i++, __a = 1)
If you compare it to Johannes' loop, you'll see that he's actually doing the same, just a bit more complicated and uglier.
C does not have an implementation of for-each
. When parsing an array as a point the receiver does not know how long the array is, thus there is no way to tell when you reach the end of the array.
Remember, in C int*
is a point to a memory address containing an int. There is no header object containing information about how many integers that are placed in sequence. Thus, the programmer needs to keep track of this.
However, for lists, it is easy to implement something that resembles a for-each
loop.
for(Node* node = head; node; node = node.next) {
/* do your magic here */
}
To achieve something similar for arrays you can do one of two things.
The following is an example of such struct:
typedef struct job_t {
int count;
int* arr;
} arr_t;
C doesn't have a foreach, but macros are frequently used to emulate that:
#define for_each_item(item, list) \
for(T * item = list->head; item != NULL; item = item->next)
And can be used like
for_each_item(i, processes) {
i->wakeup();
}
Iteration over an array is also possible:
#define foreach(item, array) \
for(int keep = 1, \
count = 0,\
size = sizeof (array) / sizeof *(array); \
keep && count != size; \
keep = !keep, count++) \
for(item = (array) + count; keep; keep = !keep)
And can be used like
int values[] = { 1, 2, 3 };
foreach(int *v, values) {
printf("value: %d\n", *v);
}
Edit: In case you are also interested in C++ solutions, C++ has a native for-each syntax called "range based for"
Here is what I use when I'm stuck with C. You can't use the same item name twice in the same scope, but that's not really an issue since not all of us get to use nice new compilers :(
#define FOREACH(type, item, array, size) \
size_t X(keep), X(i); \
type item; \
for (X(keep) = 1, X(i) = 0 ; X(i) < (size); X(keep) = !X(keep), X(i)++) \
for (item = (array)[X(i)]; X(keep); X(keep) = 0)
#define _foreach(item, array) FOREACH(__typeof__(array[0]), item, array, length(array))
#define foreach(item_in_array) _foreach(item_in_array)
#define in ,
#define length(array) (sizeof(array) / sizeof((array)[0]))
#define CAT(a, b) CAT_HELPER(a, b) /* Concatenate two symbols for macros! */
#define CAT_HELPER(a, b) a ## b
#define X(name) CAT(__##name, __LINE__) /* unique variable */
Usage:
int ints[] = {1, 2, 0, 3, 4};
foreach (i in ints) printf("%i", i);
/* can't use the same name in this scope anymore! */
foreach (x in ints) printf("%i", x);
EDIT: Here is an alternative for FOREACH
using the c99 syntax to avoid namespace pollution:
#define FOREACH(type, item, array, size) \
for (size_t X(keep) = 1, X(i) = 0; X(i) < (size); X(keep) = 1, X(i)++) \
for (type item = (array)[X(i)]; X(keep); X(keep) = 0)
If you're planning to work with function pointers
#define lambda(return_type, function_body)\
({ return_type __fn__ function_body __fn__; })
#define array_len(arr) (sizeof(arr)/sizeof(arr[0]))
#define foreachnf(type, item, arr, arr_length, func) {\
void (*action)(type item) = func;\
for (int i = 0; i<arr_length; i++) action(arr[i]);\
}
#define foreachf(type, item, arr, func)\
foreachnf(type, item, arr, array_len(arr), func)
#define foreachn(type, item, arr, arr_length, body)\
foreachnf(type, item, arr, arr_length, lambda(void, (type item) body))
#define foreach(type, item, arr, body)\
foreachn(type, item, arr, array_len(arr), body)
Usage:
int ints[] = { 1, 2, 3, 4, 5 };
foreach(int, i, ints, {
printf("%d\n", i);
});
char* strs[] = { "hi!", "hello!!", "hello world", "just", "testing" };
foreach(char*, s, strs, {
printf("%s\n", s);
});
char** strsp = malloc(sizeof(char*)*2);
strsp[0] = "abcd";
strsp[1] = "efgh";
foreachn(char*, s, strsp, 2, {
printf("%s\n", s);
});
void (*myfun)(int i) = somefunc;
foreachf(int, i, ints, myfun);
But I think this will work only on gcc (not sure).
C has 'for' and 'while' keywords. If a foreach statement in a language like C# looks like this ...
foreach (Element element in collection)
{
}
... then the equivalent of this foreach statement in C might be be like:
for (
Element* element = GetFirstElement(&collection);
element != 0;
element = GetNextElement(&collection, element)
)
{
//TODO: do something with this element instance ...
}