View previous topic :: View next topic |
Author |
Message |
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
struct in struct access |
Posted: Mon Apr 25, 2016 6:30 am |
|
|
Hi! I have to situation:
Code: |
struct {
int1 Param1;
int Param2
}str1;
struct{
int1 Value1;
int Value2;
} str2;
|
So I want to combine these structs in a new one.
Code: |
struct{
str1 First;
str2 Second;
}Main;
|
The problem is I can`t access the base struct members.
Code: |
Main.str1.Param1=1;
|
This doesn`t work. I tried a lot of combinations but I can`t achieve it!
Can you help me?
Thanks! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon Apr 25, 2016 7:10 am |
|
|
When declaring a structure in C, the name after the struct keyword, is the 'structure name', and the one at the end of the declaration, is the variable name.
The compiler ought to complain if you are trying to use the variable name as a structure name...
The syntax to declare two structures, without declaring a variable, is:
Code: |
struct str1 {
int1 Param1;
int Param2;
};
struct str2 {
int1 Value1;
int Value2;
};
|
Unless you are typedefing the structure declarations, str1 and str2, don't declare structures.
I'd also avoid using main as a structure name, since this is potentially a keyword:
Code: |
struct{
struct str1 First;
struct str2 Second;
}Both;
|
Then Both.First.Param1 merrily accesses the first parameter in the first structure. Note again you need to use the variable name, not the structure name as you are trying to do. |
|
|
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Mon Apr 25, 2016 7:24 am |
|
|
Thanks! This is working!
One more question! How can I set value to entire structure?
For example:
Code: |
struct {
int1 Param1;
int8 Param2;
}str1;
or
struct str1 {
int1 Param1;
int8 Param2;
};
|
So in this case I have a int16 var which contains all data for the structure.
How can I assign this int16 to the structure?
Or I have to do it part by part, int8 by int8, int1 by int1?
Thanks! |
|
|
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Mon Apr 25, 2016 7:28 am |
|
|
My problem is I have a few registers which contain many flags (with different length). So I want to arrange these flags into a structure and when I read the register to set value into the structure and so on... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon Apr 25, 2016 8:58 am |
|
|
There are a number of different ways of doing parts of this.
If (for instance), you have a structure like:
Code: |
struct {
int8 fred;
int8 nibble:4; //four bits
int8 cs:1; //one bit
} variable = {32,8,1};
|
Would assign 32 to fred, 8 to the four bits 'nibble', and 1 to cs.
Or you could assign a union with int16 variables to the same memory as the structure, and initialise these. |
|
|
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Mon Apr 25, 2016 11:36 am |
|
|
I need something like this:
Code: |
struct {
int8 Param1;
int8 Param2;
}str1;
unsigned int16 value;
str1=value;
|
I'm getting value from a register (16 bit wide) and I want to separate it to a structure of a bits and bytes. I have many registers with different values in length. So I need some kind of conversion int to struct.
Thanks! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon Apr 25, 2016 2:06 pm |
|
|
Use a union.
Create a union like:
Code: |
struct str_struct {
int8 Param1;
int8 Param2;
};
union {
struct str_struct str1;
unsigned int16 value;
} vals;
|
Then write your number into vals.value (the int16), and you can access vals.str1.Param1, and vals.str1.Param2 to talk to the bytes.
Look also at the : qualifier in variable declarations, as I show to access sections of a byte that at 1, 2, 3, 4 etc., bits in size. |
|
|
|