0

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]

4
  • Actually, based on your code, each array is a list of struct file_descriptors not pointers to such structs (I am not sure if that's what you meant). Anyway, it seems the issue has something to do with the open_files member--would it be possible to display the struct file_descriptor declaration? Commented May 5, 2013 at 3:41
  • sorry that was a mistake from copying from my real code. See my changes. How would I make each array a list of pointers to struct file_descriptors? Would I change it to: struct file_descriptor **list1 = (struct file_descriptor **)malloc(4096 * sizeof(struct file_descriptor *));? Commented May 5, 2013 at 3:45
  • 1
    Yes, that would be how to change it to a list of pointers. Commented May 5, 2013 at 3:50
  • @Darksky It's very simple Darksky.Look at my answer.You simply use for(i=0;i<=4096;i++) list2[i]=list1[i]; When the pointers in list2 are assigned the values of the respective pointers in list1,those pointers in list2 will point to the same variables as the pointers in list1 when dereferenced, won't they? *list1[i] are nothing but variables of type struct file_descriptor Commented May 5, 2013 at 4:00

1 Answer 1

1

list1 and list2 are of type struct file_descriptor ** not struct file_descriptor *.

Further, don't declare a struct file_descriptor pointer or variable inside a loop.

Anyways,if you want the pointers in list2 to point to the same structure variables as the respective pointers in list1 do,then it's fairly simple.You do it as:

 for(i=0;i<=4096;i++)
 list2[i]=list1[i];

Further, if you intend your arrays to be arrays of pointers to the the structure variables, instead of arrays of structure variables, then you should use the following:

   malloc(4096 * sizeof(struct file_descriptor*));  //note the second *

Edit Your line list2[i] = parent_fd; shows type mismatch because you have declared list2 as of type struct file_descriptor* and hence list2[i] is of type struct file_descriptor, while parent_fd is of type struct file_descriptor*.In simple words,the left side of the = is a struct variable while the right side is a pointer to a struct variable. Like I said, you should declare list1 and list2 as type struct file_descriptor** not struct file_descriptor*.That should deal with the warning you encountered.

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry I added open_files in my code when I shouldn't. Please see what I have now. I did exactly what you said (as I said in the question) and it does not work. I'm writing a kernel, I cannot post my entire "program" here.
While this is technically correct, I find the wording a little misleading: as it stands list1, list2 are indeed of type struct file_descriptor *. Do you mean they should be of type struct file_descriptor **? Also, I'm curious as to why you say "don't declare ... inside a loop"? While it is a bit redundant in this case, there is no harm in doing so--and definitely no harm in declaring variables inside a loop in general.
@voidptr The OP says he intends the two lists to be arrays of pointers,hence they must be of type struct file_descriptor**,since list1 and list2 are pointers.
struct file_descriptor ** fixes it. The * in struct file_descriptor * blinded me for a bit thinking it's an array of pointers. For the declaration inside the loop, I actually aren't doing so. But I put it there for the sake of the question; it's a small detail that does not require optimisation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.