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:
Callows the use of extern but most of the time is not needed - especially in simple code.- Variables are not declared with
structyou can simply writevarArray* myArr. - We don't use
malloc, you can usenew.
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;
}
You mentioned "
vs codetells me" - the Intellisense sometimes messes up. So please mention what exactly is thecompilation errorand what code doesn't work. Use https://godbolt.org/ for sharing the code :)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
Cand notCthen 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 .
