|
|
View previous topic :: View next topic |
Author |
Message |
Torello
Joined: 29 Sep 2006 Posts: 120
|
Constant structure |
Posted: Mon Feb 16, 2015 4:15 am |
|
|
Hi,
Did search, but I can't find an applicable example.
I want to create a rom structure and preload this structure with named items as not to create errors. (structure has like 40 up to 60 elements)
I've tried some formats but the only thing I get compiled OK is not what I want.
Code: | typedef struct {
int8 a;
int8 b;
int16 c;
} RomStruct_type;
RomStruct_type const Romstruct = {1,2,1234}; |
What I would like (and doesn't compile ok) is:
Code: | RomStruct_type const Romstruct = {
Romstruct.a=1,
Romstruct.b=2,
Romstruct.c=1234
};
..or also not compiling:
RomStruct_type const Romstruct = {
a=1,
b=2,
c=1234
};
|
Any thoughts? or just not possible?
Btw, i need the typedef as I plan to create a union of this structure (and 10 others) together with an byte array to code efficient read those structure rom into ram when needed. _________________ Regards, Edwin. PCWHD v5.114 |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
Re: Constant structure |
Posted: Mon Feb 16, 2015 5:09 am |
|
|
Torello wrote: | ...the only thing I get compiled OK is not what I want.
Code: | typedef struct {
int8 a;
int8 b;
int16 c;
} RomStruct_type;
RomStruct_type const Romstruct = {1,2,1234}; |
...or just not possible? |
I believe it's not possible. What you have above is correct C. The others are not syntactically correct C and won't compile. There is no way in C to specify elements by name in initialisation of a structure. Also, all elements and members of arrays must be initialised, or else none at all. Either a structure/array is initialised completely, all at once, or not at all.
Quote: |
Btw, i need the typedef as I plan to create a union of this structure (and 10 others) together with an byte array to code efficient read those structure rom into ram when needed. |
Typedef is the way to go. But be careful, complex structures, unions and arrays can give some CCS C compiler issues some problems. Being all structured and all is okay in principle, but can give practical problems. Keeping things simple is often the better way to go. Its safer to have three simple arrays than to have a array of structures each with three elements, or a structure containing three arrays.
Something that's worrying me is that on some processors (you don't say what you are using), the difference in word length between RAM and program memory creates alignment difficulties in structures - the same structure may well look different in program memory from what is does in RAM, and there is no one to one correspondence between the two.
In terms of efficiency, with the Harvard architecture (i.e. separate program and data memory spaces) most accesses to const data in program memory, other than very simple cases, have to be transferred to data memory before they can be used. It happens all the time, and not just when you want it to happen. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Mon Feb 16, 2015 5:15 am |
|
|
The one you show as working, is the standard C way of doing this. The others are not.....
What is wrong with the standard way?.
ANSI C99, does add the option to use 'designated initialisers'. CCS does not support this (they currently seem to stick around C89/90, rather than C99), but you might ask them about this. With these you could use:
RomStruct_type const Romstruct = {.a=1,.b=2,.c=1234};
Has the advantage of reminding you what value goes where. As I say, 'not supported', but if this would solve what you want, talk nicely to CCS, and since they are trying to become closer to ANSI C support, they might consider adding this. |
|
|
Torello
Joined: 29 Sep 2006 Posts: 120
|
|
Posted: Mon Feb 16, 2015 5:44 am |
|
|
Thanks All!
There's nothing wrong with the standard way.
But exactly what you say:
..Has the advantage of reminding you what value goes where..
When default value coding GPS commands structs of 40 up to 60 bytes it is more handy to directly know where the value goes. Also when having to changing it...
Of course can do with remarks also, but I thought maybe there is a official way. _________________ Regards, Edwin. PCWHD v5.114 |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|