Here is minimal test case of what I am trying to do;
// minimal test case
#include <stdlib.h>
struct array {
void *ptr;
int numElements;
int capacity;
};
struct task {
char *task_name;
int task_id;
};
#define NEW_ARRAY(TYPE, VARIABLE, CAPACITY) \
struct array VARIABLE; \
VARIABLE.ptr = malloc(CAPACITY * sizeof(TYPE)); \
VARIABLE.capacity = CAPACITY; \
VARIABLE.numElements = 0 \
#define array_for_each(TYPE, ELEM, ARRAY) \
for (int i=0, ELEM = *((TYPE *)ARRAY.ptr); \
i<ARRAY.numElements; \
i++, ELEM = *((TYPE *)ARRAY.ptr + i) \
) \
int main() {
NEW_ARRAY(struct task, my_tasks, 2);
struct task tmp;
array_for_each(struct task, tmp, my_tasks) {
;
}
return 0;
}
I am getting;
test.c:22:23: hata: incompatible types when initializing type 'int' using type 'struct task' for (int i=0, ELEM = *((TYPE *)ARRAY.ptr); \
that macro line when expanded, turnes into this:
for (int i=0, tmp = *((struct task *)my_tasks.ptr); i<my_tasks.numElements; i++, tmp = *((struct task *)my_tasks.ptr + i) ) {
;
}
Ok, so tmp is a struct task and I am casting my_tasks.ptr which is a void * to struct task * and dereferencing it, so that should give me a struct task. So I don't know why gcc is throwing me that error.
struct taskto a variable of typeint? - user253751