Skip to content

Instantly share code, notes, and snippets.

@shyiko
Forked from pancurster/for_each.c
Created February 9, 2019 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shyiko/1da758cb2b866ccd541539398e8571cf to your computer and use it in GitHub Desktop.
Save shyiko/1da758cb2b866ccd541539398e8571cf to your computer and use it in GitHub Desktop.
for_each in C
#include <malloc.h>
#include <string.h>
#include <stdio.h>
#define for_each(i, list) \
for(i = list; \
i != NULL; \
i = i->next)
struct lista {
char data;
struct lista* next;
};
int main(void)
{
struct lista l;
l.data = 'a';
l.next = malloc(sizeof(struct lista));
l.next->data = 'b';
l.next->next = NULL;
struct lista* k;
for_each(k, &l) {
printf("%c\n", k->data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment