Almost all languages have a foreach
loop (function) or something similar. I wonder if C has one? Can you post some example code?
views:
3766answers:
7There is no foreach in C.
You can use a for loop to loop through the data but the length needs to be know or the data needs to be terminated by a know value (eg. null).
char* nullTerm;
nullTerm = "Loop through my characters";
for(;nullTerm != NULL;nullTerm++)
{
//nullTerm will now point to the next character.
}
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 ...
}
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); \
count != size; \
keep=1, 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, boost has it (but C++ itself doesn't have a native for-each loop yet): Boost.ForEach
Here is a full program example of a for-each macro in C99:
#include <stdio.h>
typedef struct list_node list_node;
struct list_node {
list_node *next;
void *data;
};
#define FOR_EACH(item, list) \
for (list_node *(item) = (list); (item); (item) = (item)->next)
int
main(int argc, char *argv[])
{
list_node list[] = {
{ .next = &list[1], .data = "test 1" },
{ .next = &list[2], .data = "test 2" },
{ .next = NULL, .data = "test 3" }
};
FOR_EACH(item, list)
puts((char *) item->data);
return 0;
}
For a C++ class with size() and operator[] defined, try:
#define foreach(A,B) \
for(unsigned i=0,__a=1;i<B.size();i++,__a=1) \
for(A=B[i];__a;__a=0)
int main(){
vector v;
v.push_back(0);
v.push_back(0);
v.push_back(2);
foreach(int k,v){
cout<<k<<"\n";
}
}
This might not be the fastest, though.
This is a fairly old question, but I though I should post this. It is a foreach loop for GNU C99.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define FOREACH_COMP(INDEX, ARRAY, ARRAY_TYPE, SIZE) \
__extension__ \
({ \
bool ret = 0; \
if (__builtin_types_compatible_p (const char*, ARRAY_TYPE)) \
ret = INDEX < strlen ((const char*)ARRAY); \
else \
ret = INDEX < SIZE; \
ret; \
})
#define FOREACH_ELEM(INDEX, ARRAY, TYPE) \
__extension__ \
({ \
TYPE *tmp_array_ = ARRAY; \
&tmp_array_[INDEX]; \
})
#define FOREACH(VAR, ARRAY) \
for (void *array_ = (void*)(ARRAY); array_; array_ = 0) \
for (size_t i_ = 0; i_ && array_ && FOREACH_COMP (i_, array_, \
__typeof__ (ARRAY), \
sizeof (ARRAY) / sizeof ((ARRAY)[0])); \
i_++) \
for (bool b_ = 1; b_; (b_) ? array_ = 0 : 0, b_ = 0) \
for (VAR = FOREACH_ELEM (i_, array_, __typeof__ ((ARRAY)[0])); b_; b_ = 0)
/* example's */
int
main (int argc, char **argv)
{
int array[10];
/* initialize the array */
int i = 0;
FOREACH (int *x, array)
{
*x = i;
++i;
}
char *str = "hello, world!";
FOREACH (char *c, str)
printf ("%c\n", *c);
return EXIT_SUCCESS;
}
This code has been tested to work with gcc, icc and clang on GNU/Linux.