2

I'm trying to code an array of pointers of variables from a struct. My problem is that the variables inside the struct have different data type.

#include "stdlib.h"

typedef struct val {
 unsigned char a;
 unsigned char b;
 unsigned short c;
 unsigned int d;
} _letters;

void setup() {
 Serial.begin(9600);
}

int var1 = 0;

void loop() {
 _letters lt;
 lt.a = 1;
 lt.b = 2;
 lt.c = 3;
 lt.d = 4;

 unsigned char *ptrLetters[4];
 ptrLetters[0] = &lt.a;
 ptrLetters[1] = &lt.b;
 ptrLetters[2] = &lt.c;   //here is the problem
 ptrLetters[3] = &lt.d;  //also here

 var1 = (int)*ptrLetters[0];

 Serial.println(var1);
}

The purpose of this is because I want to save the address and access the variables from the struct (which I CAN'T modify) by the index of the array (*ptrLetters[index]), but the problem is that inside the struct there are different data types and the pointer is initialised only for char types. How do I dynamically change that?

3
  • 2
    Sidenote: ptrLetters is too small for 4 elements. Commented Nov 15, 2018 at 9:40
  • Note: in C, array indices are from 0..n-1 when size is n. So for size 3, indices are 0..2 So you need to have an array of 4 for indices 0..3. Commented Nov 15, 2018 at 9:53
  • Are you sure you can't use a single pointer to that type of struct and then dereference it to whatever element you want to retrieve or write to ? I am failing to see why there is a need for an array of pointers... Commented Nov 15, 2018 at 9:56

3 Answers 3

1

Pointer to object of any type can be implicitly converted to pointer to void (optionally cv-qualified); the pointer value is unchanged. The reverse conversion, which requires static_cast or explicit cast, yields the original pointer value:

void *ptrLetters[4];
ptrLetters[0] = &lt.a;
ptrLetters[1] = &lt.b;
ptrLetters[2] = &lt.c;   //no more problem
ptrLetters[3] = &lt.d;  //no more problem here as well

For dereferencing, in C you can simply do this:

unsigned char var1 = *((char*)ptrLetters[0]);
unsigned char var2 = *((char*)ptrLetters[1]);
unsigned short var3 = *((unsigned short*)ptrLetters[2]);
unsigned int var4 = *((unsigned int*)ptrLetters[3]);

Since you tagged this with C++ also, it is better to use static_cast in C++.

unsigned char var1 = *(static_cast<unsigned char*>(ptrLetters[0]));
unsigned char var2 = *(static_cast<unsigned char*>(ptrLetters[1]));
unsigned short var3 = *(static_cast<unsigned short*>(ptrLetters[2]));
unsigned int var4 = *(static_cast<unsigned int*>(ptrLetters[3]));
Sign up to request clarification or add additional context in comments.

4 Comments

In the structure I have only char and short data types (dont mind above, was an example), how do I convert the char to a short if I only want short data type?
You mean you want to assign char variable to a short variable?
yes, when I read the value from the pointer array has to be a short variable
char to short promotion preserves the value, so you can assign char to a short variable
0

You can use an array of void (untyped) pointers:

void *ptrLetters[4];
ptrLetters[0] = &lt.a;
ptrLetters[1] = &lt.b;
ptrLetters[2] = &lt.c;   //here is the problem
ptrLetters[3] = &lt.d;  //also here

And you can then access your variables like this:

var1 = *((char*)ptrLetters[0]);

Note that you have to cast the void pointers back to the original type first for dereferencing. The conversion to int is then performed implicitly on assignment.

Comments

0

You've not explained much on why you want to use only array of pointers or why you want to use pointers at all. Based upon what you're trying to do in your sample code, you can do it many ways.

The simplest would be to use a pointer to struct:

_letters *ptrLetter;
ptrLetter = &lt;

var1 = ptrLetter->a;

You can also explore struct of pointers or use C++ classes to contain the data and pointer together.

2 Comments

I need to use this method because I'm implementig modbus on a product that didn't have any comunication protocol, so, to get access to variables to the struct via number it's easier with a array of pointers. When the modbus recieve the index(number) and the value, with the array of pointers i can easily point the value in the struct and change the value inside
Okay, makes sense. If you want to use index-based access, using pointer to struct would not work - you should use array only as you proposed. So, use array of void pointers as given in other answers.

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.