I will try my best to explain this the best I can. I have been working in C for a bit, but have never needed to push beyond my comfort zone. I really want to try and get this working, it will help maintain a structure in my program that I have been working really hard on.
I have no formal C training though, so I am trying to teach myself as much as I can.
I have one typedef struct, let's call it "struct_A_t", and another typedef struct "struct_B_t". Within "struct_A_t" I want (amongst others) a member pointing to an array of "struct_B_t". I have multiple instances of "struct_A_t" and they don't all have the same array length for "struct_B_t" items.
I understand that passing an array to a fn passes the address to the first element, but I have also come to notice that the a pointer to the array and a pointer to the first element are not necessarily the same. Some of the pointer stuff still gets me though.
The ideal would be for me to be able to put all my instances of "struct_A_t" items in an array and pass this array around my program, changing the values of the struct_B and struct_A members as required.
See the example code below:
My main.h
#include <stdio.h>
#include "stdlib.h"
#include "stdbool.h"
#include "stdint.h"
//Defines fo the array lengths of struct_B_t
#define MyGroup0_Length 10
#define MyGroup1_Length 20
#define MyGroup2_Length 30
#define GroupCount 3
//Struct B definition
typedef struct
{
bool ItemSelected;
uint8_t ItemID;
uint8_t ItemState;
} struct_B_t;
//Struct A Definition
typedef struct
{
bool GroupEnabled;
uint8_t GroupID;
uint8_t GroupState;
uint8_t ItemCount;
struct_B_t (*ItemList)[];
} struct_A_t;
//These are the item lists that must be contained within each GroupState
struct_B_t Group0Items[MyGroup0_Length];
struct_B_t Group1Items[MyGroup1_Length];
struct_B_t Group2Items[MyGroup2_Length];
//Function prototypes
void main(void);
void BuildGroups(struct_A_t* _groups);
void ChangeGroupItems(struct_A_t* _groups, uint8_t _groupCount);
My main.c
#include <stdio.h>
#include "stdlib.h"
#include "stdint.h"
#include "stdbool.h"
#include "main.h"
//A array of type "struct_A_t" with length = 3, to accomodate my three groups
struct_A_t MyGroups[GroupCount];
void main(void)
{
BuildGroups(&MyGroups);
ChangeGroupItems(&MyGroups, GroupCount);
}
void BuildGroups(struct_A_t* _groups)
{
//Set the group lists
//Group 1
_groups[0].ItemList = &Group0Items;
_groups[0].ItemCount = MyGroup0_Length;
//Group 2
_groups[1].ItemList = &Group1Items;
_groups[1].ItemCount = MyGroup1_Length;
//Group 3
_groups[2].ItemList = &Group2Items;
_groups[2].ItemCount = MyGroup2_Length;
}
void ChangeGroupItems(struct_A_t* _groups, uint8_t _groupCount)
{
for(uint8_t i = 0; i < _groupCount; i++)
{
for(uint8_t j = 0; j < _groups[i].ItemCount; j++)
{
if(_groups[i].ItemList[j].ItemSelected)
{
_groups[i].ItemList[j].ItemState++;
}
}
}
}
As you can guess, this is not compiling correctly. When I change things around a bit and do get this to build, I get warnings for incompatible pointer types.
I don't feel like this is an extremely unique problem, so I would love to hear any suggestions. At this stage, I am trying to keep my application data confined to one struct, and I want to avoid making too many things globally accessible.
I also should note, my actual program looks a bit different, and things are split into more .c files than this, and what I am trying to achieve will fit nicely into my overall program pattern.
Thanks in advance.