typedef struct {
int head
} List;
List* array[10];
int size = 0;
List* create() {
if(size < 10){
List list1;
list1.head = size;
array[size] = &list1;
printf("%d \n", array[size]->head); //value is stored perfectly here
printf("%p \n", array[size]); //same value printed everytime (e.g 0x7ffeee1139f0 )
return array[size++];
}
return NULL;
}
int main() {
List* plist1 = List_create();
printf("%p \n", plist1);//same value 0x7ffeee1139f0
List* plist2 = List_create();
printf("%p \n", plist2);//same value 0x7ffeee1139f0
}
For the following code above, when I try to iterate over the array and print the value at each index, which should be the address of each list struct I created and hence should be unique.
But I get the same address stored at each index of the array. I can't figure out why.
Also when I iterate over the array and print array[index]->head, I get some garbage value(e.g. -477361872).
Does anyone know what am I doing wrong?