alexz
Full Member level 5
struct assignment
A well known method to make a code flexible and divide the project in to objects is to use pointers to functions in a struct and then initialize the struct with pointers to the functions you want to use.
typedef struct myStructType
{
void (*myFunc)(void);
}myStruct;
void func1(void)
{
}
void main(void)
{
myStruct struct1;
struct1.myFunc = func1; //assign the func pointer
struct1.myFunc(); //call function func1
}
This allows using the same struct for different functions at a time.
Now, the question is how to do the similar thing to other structs rather then to functions.
I want to have a pointer to a struct in a struct. And then being able to initialize this pointer to a struct I want to use.
struct oneStruct
{
char var1;
}one;
struct twoStruct
{
char var1;
}two;
typedef struct basic
{
structType structPtr; //??????????????
}structBasic;
void main(void)
{
structBasic.structPtr = &oneStruct; //set the pointer to a struct I want to use
structBasic.structPtr->var1++;
}
A well known method to make a code flexible and divide the project in to objects is to use pointers to functions in a struct and then initialize the struct with pointers to the functions you want to use.
typedef struct myStructType
{
void (*myFunc)(void);
}myStruct;
void func1(void)
{
}
void main(void)
{
myStruct struct1;
struct1.myFunc = func1; //assign the func pointer
struct1.myFunc(); //call function func1
}
This allows using the same struct for different functions at a time.
Now, the question is how to do the similar thing to other structs rather then to functions.
I want to have a pointer to a struct in a struct. And then being able to initialize this pointer to a struct I want to use.
struct oneStruct
{
char var1;
}one;
struct twoStruct
{
char var1;
}two;
typedef struct basic
{
structType structPtr; //??????????????
}structBasic;
void main(void)
{
structBasic.structPtr = &oneStruct; //set the pointer to a struct I want to use
structBasic.structPtr->var1++;
}