foreach-c/foreach.h
2023-11-17 14:14:55 -04:00

11 lines
573 B
C

#pragma once
/*
* Need to pass in the size of the array, since pointer arrays don't know their size
* Can use sizeof(array) / sizeof(type) to get the size of the array if it is not a pointer
* Decays everything into pointers, as you cannot assign two different types in a for loop declaration
* For example, cannot do int* iter, int item = *iter, as they are different types
* You need to dereference the item, as it is a pointer to the current element
*/
#define foreach(type, item, array, size) \
for (type* item = array; item < array + size; item++)