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 7178691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:58:16+00:00 2026-05-28T16:58:16+00:00

I was given some code by our professor with some things I have never

  • 0

I was given some code by our professor with some things I have never seen before. I am trying to do my due diligence before I bother (and possibly embarass myself) my professor on the lower level stuff.

First a structure is defined.

struct entry {
    char *lexptr;
    int token;
};

Then at another point, an array is defined and initialized.

struct entry keywords[] = {
   "div", DIV,
   "mod", MOD,
   0 , 0
};

Where DIV and MOD are #define directives. To my surprise, everything compiles and runs.

Personally, I would have done something novice like..

struct entry keywords[3];

keywords[0]->lexptr="div";
keywords[0]->token=DIV;
//and so on

My first question is, how does that init method work like it does? My best guess is that address location of the array match the order of the elements that were defined in the structure. So the element definintion {"div", DIV,"mod", MOD, 0 , 0}; would nicely fit three elements of entry on the address level.

My second question is, is this normal? Why this way vs my sliglty more readable, yet more verbose, version of initializing the array? Are there any advantages to the first method?

  • 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-28T16:58:17+00:00Added an answer on May 28, 2026 at 4:58 pm

    It might be preferable if it was written:

    const struct entry keywords[] =
    {
       { "div", DIV },
       { "mod", MOD },
       {     0,   0 }
    };
    

    which uses braces around each sub-structure (and there should arguably be a const on the pointer in the structure definition as well as on the array), but this is how you’d initialize a static array of structures, and it can be used to initialize local arrays too.

    Also, in C99, you could use designated initializers:

    const struct entry keywords[] =
    {
       [0] = { .lexptr = "div", .token = DIV },
       [1] = { .lexptr = "mod", .token = MOD },
       [2] = { .lexptr =     0, .token =   0 }
    };
    

    In this example, there is no advantage to the notation (I would not use it), but in more complex cases where, perhaps, only a few of the many fields in a structure need to be initialized in each row, the designated initializers might have an advantage. You can also have initializers out of sequence. Again, you would not use that here, but if you have an enum for the entries, you might make use use of [ENUMX] = { ... }, [ENUMY] = { ... }, ... without needing to know the number corresponding to ENUMX and ENUMY. (NB: I could have used the indexing designations without the member designations, or the member designations without the indexing designations; I showed both to illustrate the range of options.)

    It is much better to use initialization than the executable code that you show as an alternative. The initialization of static or global arrays is done by the system as the program is loaded, so it reduces the execution time, and reduces the volume of code in the executable (both desirable). It also allows the structures to be made constant, so that they can’t be changed at runtime. Assignment requires the structure to be modifiable.

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

Sidebar

Related Questions

I've been given some code with commenting unlike anything I've come across before: //{{{
I have some code which needs to do things based on a schedule: e.g.
Given some JS code like that one here: for (var i = 0; i
I've been give some lovely Java code that has a lot of things like
I have some code that gives a user id to a utility that then
I have a some code that gets passed a class derived from a certain
The following piece of code was given to us from our instructor so we
I have an intermittent problem with some code that writes to a Windows Event
I am trying to write some c# code to start a browser using Process.Start(app,args);
I am trying to write a unit test to cover code found in our

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.