'Setting a pointer to a specific array and/or struct element
Here is the scenario:
I have an array, AllElements
of structure ElementSet
, and inside of that structure - is a set of myElement
structures and also an array of myElement
structures. This is basically serving as an organized static data collection of which I sometimes need to access an individual member of.
Here are the code snippets of the declarations:
In Header (.h) File:
typedef struct myElement
{
uint32 then;
uint32 now;
bool isInitialized;
} myElement;
In a function in the Program (.c) File:
const int8 numElements = 8;
const int8 numSubElements = 4;
static struct ElementSet {
struct myElement CN;
struct myElement CC[numSubElements];
struct myElement CP;
struct myElement CR;
} ElementSet;
static struct ElementSet AllElements[numElements];
I have another function in which I need to pass a pointer to a myElement
struct to perform an operation.
So, I have to create a pointer to a myElement
struct, easy enough:
struct myElement *thisElement
Now I select my element based on inputs passed to the function. So, I may need to, for example, access the data in the element defined by, for example, AllElements[3].CN
, or AllElements[7].CC[3]
Are the assignments this simple?
thisElement = &AllElements[3].CN;
thisElement = &AllElements[7].CC[3];
Or is there something different I need to do to make sure I'm accessing exactly the element I need?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|