LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2015, 05:46 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
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
 
Old 04-08-2015, 06:35 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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).

Last edited by johnsfine; 04-08-2015 at 06:42 AM.
 
Old 04-08-2015, 07:54 AM   #3
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
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.
 
Old 04-08-2015, 08:09 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Try this:
Code:
typedef struct xtime {
    unsigned int x;
    unsigned int y;
} XTIME;

static const XTIME a = { 10, 10 };
static const XTIME b = { 5, 5 };

static const XTIME *asstruct[2] = {
    &a,
    &b
};

Last edited by NevemTeve; 04-08-2015 at 08:10 AM.
 
Old 04-08-2015, 08:17 AM   #5
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
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
 
Old 04-08-2015, 10:17 AM   #6
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
This will work:
Code:
        XTIME asstruct[] = {
                { 10, 10 },
                { 5, 5 }
        };


        XTIME a = asstruct[0];
        XTIME b = asstruct[1];
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
Does calling an element of an array calls the whole array unkn(0)wn Programming 3 07-06-2012 07:50 PM
[SOLVED] [C++] error: array must be initialized with a brace-enclosed initializer MTK358 Programming 11 07-01-2011 05:50 PM
error: initializer element is not constant samrat_rao Programming 6 12-03-2008 01:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration