Home > OS >  Pointer to element in array of pointers. & result in dereference instead of reference
Pointer to element in array of pointers. & result in dereference instead of reference

Time:10-15

I am relatively inexperienced on C/C and (obviously) got confused with pointers.

I have an array of pointers to a structure. I want to initialize a pointer to an element of the array of pointers. I expect to be able to do something like this:

struct mystruct **pt = &(structarray[i])

VS Code however tells me that the operators are mystruct ** and mystruct. How is this possible? If I try to write reduced examples, it works as I imagine, but in the code I wrote it does not.


If more context is needed, below is the long explanation. I have a structure like:

struct varArray {
  struct varArray *next;
  short unsigned int imin;
  short unsigned int imax;
  struct varNode varNode[1];
};

I define a pointer array as a global variable:

extern struct varArray *varPlane;

Next I initialize all elements of the array to a null pointer:

struct varArray * varPlane[nmax];
for (int i = 0; i < nmax; i  ) {
 varPlane[i] = nullptr;
}

Under some conditions I want a varPlane element to point to a structure that I initialize with malloc. So I would like to do:

struct varArray ** temparray =  &(varPlane[INDEX(ix, iy)]);
*temparray = (struct varArray *) malloc(sizeof(struct varArray)   sizeof(struct varNode) * (imax - imin));

But the first line doesn't work for the reasons I mentioned above.

I need to use a pointer to the varPlane element because I create a linked list iteratively by doing:

temparray=(*temparray)->next

CodePudding user response:

Whoever teaches you C teaches you C - just to be clear on that. Current best practices are far different between the languages.

These are guidelines, not rules:

  1. C allows the use of extern but most of the time is not needed - especially in simple code.
  2. Variables are not declared with struct you can simply write varArray* myArr.
  3. We don't use malloc, you can use new.

This is compiled (x64 msvc v19.latest): https://godbolt.org/z/od84coMTz

#include <iostream>

struct varNode{
    int a;
};

struct varArray {
  varArray *next;
  short unsigned int imin;
  short unsigned int imax;
  varNode node[1];
};

int main()
{
    const auto nmax = 5;

    varArray * plane[nmax];
    for (int i = 0; i < nmax; i  ) 
    {
        plane[i] = nullptr;
    }
    varArray ** temparray =  &(plane[1]);
    std::cout << temparray;
}
  1. You mentioned " vs code tells me" - the Intellisense sometimes messes up. So please mention what exactly is the compilation error and what code doesn't work. Use https://godbolt.org/ for sharing the code :)

  2. As you see I simplified the C style code to a more C manner. Hopefully I didn't discard anything important. If the case is that you need C and not C then most of the pointer semantics are the same just the syntax differs sometimes (Like in the malloc).

CodePudding user response:

First things first the program is invalid and won't compile because the type of varPlane in the declaration and definition are different. In particular, extern struct varArray *varPlane declares a pointer named varPlane to a struct named varArray. On the other hand, struct varArray * varPlane[nmax]; defines an array named varPlane with size nmax and elements of type varArray*.

That is, the program is invalid C .

  • Related