How to assign a global generic pointer to a user defined structure

Hi everyone

I have a sketch that declares 7 identical structures named ptPoint1, ptPoint2 .... ptPoint 7 in a global .H file.

The sketch needs to manipulate just one of these structures depending on the user input selection. My normal attitude would be to have a global structure pointer of the same type, and assign the pointer
to it to a globally defined pointer, so I can then use simple code to manipulate that structure.

However, whatever I try, I can find no way of generating a global pointer of the same type as the structures in the Arduino, it constantly generates errors. I cn use the individual pptPoint1, pptPoint2 pointers of course, but that will stillrequire a lot of switch statements etc in most functions to choose the correct one.

My wish is to have an identical structure declared, lets call it staticptstruct. This will also be declared with a pointer *pstaticptstruct. I do not understand why as the structures are identical why I cannot use something like

pstaticpt = pptpoint1; where pptpoint1 is declared as a pointer with the structure declaration.

.h file contents :-

struct {

.......... stuff

} *pstaticptstruct;

struct {

.......... same stuff

} ptPoint1, *pptPoint1;
struct {

.......... same stuff

} ptPoint21, *pptPoint2;
struct {

.......... same stuff

} ptPoint3, *pptPoint3;

...... up to 7 times

You get the idea ?

As the structure that is declared for the pointer *pstaticptstruct is IDENTICAL to all the ptPointx structures, I cannot see why I cannot assign that pointer to any other one of the pptPointx structures, (or rather the structures address)

pstaticptstruct = &ptPoint2 fails
pstaticptstruct = pptPoint2 fails
*pstaticptstruct = ptPoint2 fails
*pstaticptstruct = &ptPoint2 fails

I am trying to avoid a horrendous overburden of checks throughout the various functions to find out WHICH point structure is the relevant one with large switch case etc which will hugely bloat the code..

Can anyone tell me how I can achieve this relatively simple task in the Arduino environment ?

Many thanks in advance.

Ian

Why don't you make an array of the structs?

Hi gfvalvo

Good suggestion. It is certainly "A" way to overcome the issue, but I would prefer to be able to simply set a pointer to the relevant strcuture as it will match the rest of my exisitng code !!!

I will certainly bear your suggestion in mind - Thanks a lot for your rapid response..

Is this what you want?

struct myStructType {
  int field1;
  char field2;
};

myStructType struct1, struct2, struct3, struct4, *structPtr;

structPtr = &struct1; // or &struct2, &struct3, .....

gfvalvo:
Is this what you want?

struct myStructType {

int field1;
  char field2;
};

myStructType struct1, struct2, struct3, struct4, *structPtr;

structPtr = &struct1; // or &struct2, &struct3, .....

but why? If the structs are "identical" you don't need to define the struct with several names.

It seems it is just data...

BulldogLowell:
but why? If the structs are "identical" you don't need to define the struct with several names.

I was defining several instances of the struct, all of the same type. Thought that is what OP wanted.

Thank you all for your responses, they are all much appreciated.

gfvalvo's "cure" by declaring the structures as he stated does the job perfectly. I can then initialise them individually, but more importantly, I can select the right pointer, based on the content of one the internal fields, the Point number, and then extrapolate that out to the required structure or pointer for the rest of the processing, which is exactly what i needed to do.

Best
Ian

chopperaddict:
which is exactly what i needed to do.

exactly, one struct of many instances... move the pointer to the instance you wish to manipulate...

gfvalvo:
I was defining several instances of the struct, all of the same type. Thought that is what OP wanted.

no, I was underscoring what you did... not several structs, but one with several instances...

chopperaddict:
Thank you all for your responses, they are all much appreciated.

gfvalvo's "cure" by declaring the structures as he stated does the job perfectly. I can then initialise them individually, but more importantly, I can select the right pointer, based on the content of one the internal fields, the Point number, and then extrapolate that out to the required structure or pointer for the rest of the processing, which is exactly what i needed to do.

Best
Ian

Now that you have that, revisit my suggestion from Reply #1. Put them in an array:

struct myStructType {
  int field1;
  char field2;
};

myStructType structArray[5], *structPtr;

Then, you can access them using either array or pointer notation:

// This way:
x = structArray[1].field1;

// Or this way:
x = (structArray+1)->field1;

You can assign pointers to anything if you declare them void * and typecast things.

Jimmus:
You can assign pointers to anything if you declare them void * and typecast things.

Yeah....I think that's a bit over chopperaddict's head right now.

No offence taken to your comment :-

"Yeah....I think that's a bit over chopperaddict's head right now."

Not so, although I will admit to being pretty rusty on my"C", and I had forgotten that trick, and yes, I do know how to (cast) items to use it :slight_smile: :slight_smile:

The new code is developing well now, doing it exactly as I wanted to, using a singlepointer to address various similar structures

Thanks everyone.
Ian