I have two arrays, each initialised as follows:
struct file_descriptor *list1 = (struct file_descriptor *)malloc(4096 * sizeof(struct file_descriptor));
struct file_descriptor *list2 = (struct file_descriptor *)malloc(4096 * sizeof(struct file_descriptor));
Each array is essentially a list of struct file_descriptor pointers.
Now list1 already has a pointers in the array (that point to struct file_descriptor's), and I need to have the same indices in list2 point to the same objects that list1 pointers point to. So if list1[2] = some_object, I'm trying to point list2[2] = some_object as well.
So:
int i;
for(i = 3; i < 4096; i++) {
struct file_descriptor *parent_fd = &list1[i]; // points to the struct that element at i points to in list1
list2[i] = parent_fd;
}
At line 2 in the loop, this throws a:
incompatible types when assigning to type ‘struct file_descriptor’ from type ‘struct file_descriptor *’ error.
Obviously, that's because list2[i] is the actual struct file_descriptor, and not the pointer in the list2 which points to that struct. Changing it to &list2[i] = parent_fd; also throws an error because &list2[i] is not an lvalue.
How would I dereference the pointer in the array at index i, so that list2[i] points to *list1[i]
struct file_descriptorsnot pointers to such structs (I am not sure if that's what you meant). Anyway, it seems the issue has something to do with theopen_filesmember--would it be possible to display thestruct file_descriptordeclaration?struct file_descriptors? Would I change it to:struct file_descriptor **list1 = (struct file_descriptor **)malloc(4096 * sizeof(struct file_descriptor *));?for(i=0;i<=4096;i++) list2[i]=list1[i];When the pointers inlist2are assigned the values of the respective pointers inlist1,those pointers inlist2will point to the same variables as the pointers inlist1when dereferenced, won't they?*list1[i]are nothing but variables of typestruct file_descriptor