hi all, Structures in C are supposed to be a contiguous block of memory. But what if I need my structure to contain some data variables in RAM, and some constant that can be located in ROM? I don't want to leave the constant in RAM to waste its space. How can such a struct be declared? Thanks
How can such a struct be declared? It can't, and arguably that's a Good Thing. If a collection of elements contains both ROM constants and RAM variables, it's not good coding practice to put all of it into a single struct. Put them in two separate structs instead. The RAM struct may usefully contain a pointer to the ROM struct then, to express the link between the two.
I think it would've been much more convenient if the two can be combined inside a single struct. Now we need two structs, and pointers linking the two; getting unecessarily complicated. If C language does not support this method, do you think it's because it is not used often, or a bad practice? what specific aspect of it is "bad"?
THINK contents of a struct is addressed by base and offset. So to have a struct of both RAM and ROM data all addressing would have to be changed to pointers i.e. the struct would not contain data, but pointers to data. Eureka, if you make your struct a struct of pointers, the pointers can point to either RAM or ROM data. Erik
"I think it would've been much more convenient if the two can be combined inside a single struct." Then it couldn't be a single structure, could it? "If C language does not support this method, do you think it's because it is not used often, or a bad practice? what specific aspect of it is 'bad'?" As Erik said, THINK about it: a structure is by definition a single contiuous block of memory; we're not talking about some "feature" or "limitation" of the 'C' language - that's the very nature of the beast! If it were simply a limitation of 'C', you'd be able to write your own implementation in Assembler; but think about it again: how could you have a single label in Assembler that allows you to access both XRAM and ROM...? "Now we need two structs, and pointers linking the two" You don't necessarily need pointers - it was just suggested that it might be a useful way to link them.