Skip to content

Instantly share code, notes, and snippets.

@dmbfm
Created August 1, 2020 15:42
Show Gist options
  • Save dmbfm/20ebfddc0248c0206eff06555568b973 to your computer and use it in GitHub Desktop.
Save dmbfm/20ebfddc0248c0206eff06555568b973 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define length(arr) (sizeof(arr)/sizeof(arr[0]))
#define TOKEN_CONCAT_INNER(x, y) x##y
#define TOKEN_CONCAT(x, y) TOKEN_CONCAT_INNER(x, y)
#define LABEL(id) TOKEN_CONCAT(LABEL_##id##_, __LINE__)
#define foreach(type, name, arr) \
for(int i = 0; i < length(arr); ++i) \
for(type *name;;)\
if (1)\
{\
name = &arr[i];\
goto LABEL(block);\
LABEL(done):\
break;\
}\
else\
while(1)\
if(1)\
{\
goto LABEL(done);\
}\
else\
LABEL(block):\
typedef struct Point
{
float x;
float y;
} Point;
int main(void) {
int arr[100] = {0};
Point points[100] = {0};
foreach(int, curr, arr)
{
*curr = i;
}
foreach(int, curr, arr)
{
printf("%d\n", *curr);
}
foreach(Point, p, points)
{
p->x = i;
p->y = i+1;
}
foreach(Point, p, points)
{
printf("%f, %f\n", p->x, p->y);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment