Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7560463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:54:33+00:00 2026-05-30T12:54:33+00:00

When I tried to initialize a global array that contains structure elements, I got

  • 0

When I tried to initialize a global array that contains structure elements, I got the ” error: initializer element is not constant”

a.h


#define MAX_MSG_NUM 20
#define NAME_SIZE   15
#define MAX_QUE_NUM  10

typedef struct {
   int         index;
   int         tid;
   int         front;
   int         rear;
   char        name[NAME_SIZE];
   char        msgbuf[MAX_MSG_NUM];
} THREAD;

typedef enum {
   I1        = 0,
   I2        = 1,
   I3        = 2,
   I4        = 3
} DMTHREAD;

a.c


THREAD   a[MAX_MSG_NUM];
THREAD   b[MAX_MSG_NUM];
THREAD   c[MAX_MSG_NUM];

THREAD   T[MAX_QUE_NUM] = {
   {I1,   0, 0, 0, "CONFIG1",      a[MAX_MSG_NUM]},
   {I2,   0, 0, 0, "CONFIG2",      b[MAX_MSG_NUM]},
   {I3,   0, 0, 0, "CONFIG3",       c[MAX_MSG_NUM]},
   0
};

GCC compiler.

bash-3.2$ gcc -g a.h a.c
a.c:8: error: initializer element is not constant
a.c:8: error: (near initialization for 'T[0].msgbuf[0]')
a.c:9: error: initializer element is not constant
a.c:9: error: (near initialization for 'T[1].msgbuf[0]')
a.c:10: error: initializer element is not constant
a.c:10: error: (near initialization for 'T[2].msgbuf[0]')

Thanks for help.

Enlightened by your answers, I changed the code and it works fine now.

MSG   T[MAX_QUE_NUM] = {
           {I1,   0, 0, 0, "CONFIG1",      {0}},
           {I2,   0, 0, 0, "CONFIG2",      {0}},
           {I3,   0, 0, 0, "CONFIG3",      {0}}, 
           0,
    }

Thank you for your answers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:54:34+00:00Added an answer on May 30, 2026 at 12:54 pm

    a[MAX_MSG_NUM] is indeed non-constant. It also denotes a Thread instance, which happens to be outside the array you defined earlier, and does not match the type you should pass there (char[]).

    So it is wrong on so many levels.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a structure that contains an arrays of another structure, it looks something
How do you use a file to initialize a global const object that is
tried to research on that but sometimes I seem to lack some googling skills...
I seem to have an array scope issue. I have a global variable; var
is it possible to declare a variable length array with global scope in objective-c?
I'm trying to create a global array whose size is determined by an external
Tried to map it from Preferences -> Settings -> Keyboard, but the key combo
Tried something like this: HttpApplication app = s as HttpApplication; //s is sender of
Tried following the instructions here: How to use Google app engine with my own
Tried this: $('.link').click(function(e) { $.getScript('http://www.google.com/uds/api?file=uds.js&amp;v=1.0', function() { $('body').append('<p>GOOGLE API (UDS) is loaded</p>'); }); return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.