Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Foreach loop
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== C === The [[C (programming language)|C]] language does not have collections or a foreach construct. However, it has several standard data structures that can be used as collections, and foreach can be made easily with a [[C macro|macro]]. However, two obvious problems occur: * The macro is unhygienic: it declares a new variable in the existing scope which remains after the loop. * One foreach macro cannot be defined that works with different collection types (e.g., array and linked list) or that is extensible to user types. C string as a collection of char <syntaxhighlight lang="c" highlight="4-6" line> #include <stdio.h> /* foreach macro viewing a string as a collection of char values */ #define foreach(ptrvar, strvar) \ char* ptrvar; \ for (ptrvar = strvar; (*ptrvar) != '\0'; *ptrvar++) int main(int argc, char** argv) { char* s1 = "abcdefg"; char* s2 = "123456789"; foreach (p1, s1) { printf("loop 1: %c\n", *p1); } foreach (p2, s2) { printf("loop 2: %c\n", *p2); } return 0; } </syntaxhighlight> C int array as a collection of int (array size known at compile-time) <syntaxhighlight lang="c" highlight="4-6" line> #include <stdio.h> /* foreach macro viewing an array of int values as a collection of int values */ #define foreach(intpvar, intarr) \ int* intpvar; \ for (intpvar = intarr; intpvar < (intarr + (sizeof(intarr)/sizeof(intarr[0]))); ++intpvar) int main(int argc, char** argv) { int a1[] = {1, 1, 2, 3, 5, 8}; int a2[] = {3, 1, 4, 1, 5, 9}; foreach (p1, a1) { printf("loop 1: %d\n", *p1); } foreach (p2, a2) { printf("loop 2: %d\n", *p2); } return 0; } </syntaxhighlight> Most general: string or array as collection (collection size known at run-time) : ''{{code|idxtype}} can be removed and <code>[[typeof]](col[0])</code> used in its place with [[GNU Compiler Collection|GCC]]'' <syntaxhighlight lang="c" highlight="5-8" line> #include <stdio.h> #include <string.h> /* foreach macro viewing an array of given type as a collection of values of given type */ #define arraylen(arr) (sizeof(arr)/sizeof(arr[0])) #define foreach(idxtype, idxpvar, col, colsiz) \ idxtype* idxpvar; \ for (idxpvar = col; idxpvar < (col + colsiz); ++idxpvar) int main(int argc, char** argv) { char* c1 = "collection"; int c2[] = {3, 1, 4, 1, 5, 9}; double* c3; int c3len = 4; c3 = (double*)calloc(c3len, sizeof(double)); c3[0] = 1.2; c3[1] = 3.4; c3[2] = 5.6; c3[3] = 7.8; foreach (char, p1, c1, strlen(c1)) { printf("loop 1: %c\n", *p1); } foreach (int, p2, c2, arraylen(c2)) { printf("loop 2: %d\n", *p2); } foreach (double, p3, c3, c3len) { printf("loop 3: %.1lf\n", *p3); } return 0; } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)