C array of structures keeps saying initializer element not constant
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C array of structures keeps saying initializer element not constant
Hi All,
after several years working on C++ I need to do some work in C and producing an array of structures is driving me mad, can't believe I've forgotten this so quickly! Basically I have a structure, I then need an array of these, I don't want an array of pointers to the structures just a "list" of these arrays. For my simple example I have
Code:
typedef struct xtime
{
unsigned int x;
unsigned int y;
}XTIME;
XTIME a = { 10, 10 };
XTIME b = { 5, 5 };
XTIME asstruct[ 2 ] =
{
a,
b
};
Source/List.c:38: initializer element for `asstruct[0]' is not constant
Source/List.c:40: initializer element for `asstruct[1]' is not constant
Can anyone advise please?
Thanks in advance
edit.
Should add the reason I'm doing it this way is in real code it's a large structure which can be repeated many times. I can't use a list of pointers as a couple of elements need to change each time. So for example the struct might be ITEM A = { details, ...., number, location } all that changes is number and location. It's long winded to fill in the same details eg { "blah, .......} for each element, and a pointer will overwrite the master item for each change.
Thanks again
Last edited by knobby67; 04-08-2015 at 06:02 AM.
Reason: added more info
The usual C solution to this kind of problem is #define
#define is a horribly flawed construct for that purpose. All the places where #define is the only easy choice in C (despite its flaws) are indications that C++ is a better language.
I can't tell whether #define is your best choice, because I can't see your entire project. But I expect it is the only easy choice from where you are now.
Code:
#define a { 10, 10 }
etc.
Another approach would be to use run time code to initialize the array, rather than compile time. That could be an initializer list if the array is located inside a function (rather than global or static). Or the array could be global an uninitialized and have separate code to initialize it.
BTW, if you use (abuse) #define that way, you may want to have all those #define's and the global definition of that array all isolated in a separate .c file. So the #define's pollute the name space of only that .c file, rather than a wider part of your project. Then the rest of the project could see that array through an extern directive (which should be in a .h file).
Thanks,
I think the only to do it is some pointer jiggery pokery. But have say rather than item_a, point to item_a1, item_a2, Then most of the structure is the same at set up, but the couple of elements I need to change can be without over writing a single "master element structure.
just making a and b const should do the trick, i think
why not define those values directly in the array ?
if you plan to change them later in the program by using "a" and "b" as handles, then you are doing it wrong
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.