How to initialize a struct in accordance with C programming language standards

The Question :

487 people think this question is useful

I want to initialize a struct element, split in declaration and initialization. This is what I have:

typedef struct MY_TYPE {
  bool flag;
  short int value;
  double stuff;
} MY_TYPE;

void function(void) {
  MY_TYPE a;
  ...
  a = { true, 15, 0.123 }
}

Is this the way to declare and initialize a local variable of MY_TYPE in accordance with C programming language standards (C89, C90, C99, C11, etc.)? Or is there anything better or at least working?

Update I ended up having a static initialization element where I set every subelement according to my needs.

The Question Comments :
  • you really should accept a better answer, I see you had to use some bad coding guide, but you still shouldn’t suggest to other people that that is the right way to do it..
  • @KarolyHorvath well, most of the good answers are specific to C99. Maybe my question is a duplicate of stackoverflow.com/questions/6624975/… ?
  • if that was your original intention, then probably yes, but then 1) the votes would be very misleading. 2) from the top search hits this is the only one which shows the C99 way….. it would be better to re-use this page for C99 demonstration… (apparently people started to link this page to show how to do it)
  • Interesting that the accepted (and heavily upvoted) answer doesn’t actually answer the question, even as originally posted. Designated initializers don’t address the OP’s problem, which is to split the declaration from the initialization. For pre-1999 C, the only real solution is to assign to each member; for C99 and later, a compound literal, as in CesarB’s answer, is the solution. (Of course an actual initializer, with or without designators, would be even better, but apparently the OP was saddled with a really bad coding standard.)
  • Strictly speaking, the term “ANSI C” now refers to the 2011 ISO standard, which ANSI has adopted. But in practice the term “ANSI C” commonly refers to the (officially obsolete) 1989 standard. For example, “gcc -ansi” still enforces the 1989 standard. Since it’s ISO that published the 1990, 1999, and 2011 standards, it’s best to avoid the term “ANSI C”, and to refer to the date of the standard if there’s any possibility of confusion.

The Answer 1

757 people think this answer is useful

In (ANSI) C99, you can use a designated initializer to initialize a structure:

MY_TYPE a = { .flag = true, .value = 123, .stuff = 0.456 };

Edit: Other members are initialized as zero: “Omitted field members are implicitly initialized the same as objects that have static storage duration.” (https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html)

The Answer 2

205 people think this answer is useful

You can do it with a compound literal. According to that page, it works in C99 (which also counts as ANSI C).

MY_TYPE a;

a = (MY_TYPE) { .flag = true, .value = 123, .stuff = 0.456 };
...
a = (MY_TYPE) { .value = 234, .stuff = 1.234, .flag = false };

The designations in the initializers are optional; you could also write:

a = (MY_TYPE) { true,  123, 0.456 };
...
a = (MY_TYPE) { false, 234, 1.234 };

The Answer 3

97 people think this answer is useful

I see you’ve already received an answer about ANSI C 99, so I’ll throw a bone about ANSI C 89. ANSI C 89 allows you to initialize a struct this way:

typedef struct Item {
    int a;
    float b;
    char* name;
} Item;

int main(void) {
    Item item = { 5, 2.2, "George" };
    return 0;
}

An important thing to remember, at the moment you initialize even one object/ variable in the struct, all of its other variables will be initialized to default value.

If you don’t initialize the values in your struct, all variables will contain “garbage values”.

Good luck!

The Answer 4

43 people think this answer is useful

a = (MYTYPE){ true, 15, 0.123 };

would do fine in C99

The Answer 5

23 people think this answer is useful

You’ve almost got it…

MY_TYPE a = { true,15,0.123 };

Quick search on ‘struct initialize c’ shows me this

The Answer 6

20 people think this answer is useful

C programming language standard ISO/IEC 9899:1999 (commonly known as C99) allows one to use a designated initializer to initialize members of a structure or union as follows:

MY_TYPE a = { .stuff = 0.456, .flag = true, .value = 123 };

It is defined in paragraph 7, section 6.7.8 Initialization of ISO/IEC 9899:1999 standard as:

If a designator has the form
. identifier
then the current object (defined below) shall have structure or union type and the identifier shall be the name of a member of that type.

Note that paragraph 9 of the same section states that:

Except where explicitly stated otherwise, for the purposes of this subclause unnamed members of objects of structure and union type do not participate in initialization. Unnamed members of structure objects have indeterminate value even after initialization.

In GNU GCC implementation however omitted members are initialized as zero or zero-like type-appropriate value. As stated in section 6.27 Designated Initializers of GNU GCC documentation:

Omitted field members are implicitly initialized the same as objects that have static storage duration.

Microsoft Visual C++ compiler should support designated initializers since version 2013 according to official blog post C++ Conformance Roadmap. Paragraph Initializing unions and structs of Initializers article at MSDN Visual Studio documentation suggests that unnamed members initialized to zero-like appropriate values similarly to GNU GCC.

ISO/IEC 9899:2011 standard (commonly known as C11) which had superseded ISO/IEC 9899:1999 retains designated initializers under section 6.7.9 Initialization. It also retains paragraph 9 unchanged.

New ISO/IEC 9899:2018 standard (commonly known as C18) which had superseded ISO/IEC 9899:2011 retains designated initializers under section 6.7.9 Initialization. It also retains paragraph 9 unchanged.

The Answer 7

14 people think this answer is useful

as Ron Nuni said:

typedef struct Item {
    int a;
    float b;
    char* name;
} Item;

int main(void) {
    Item item = {5, 2.2, "George"};
    return 0;
}

An important thing to remember: at the moment you initialize even one object/variable in the struct, all of its other variables will be initialized to default value.

If you don’t initialize the values in your struct (i.e. if you just declare that variable), all variable.members will contain “garbage values”, only if the declaration is local!

If the declaration is global or static (like in this case), all uninitialized variable.members will be initialized automatically to:

  • 0 for integers and floating point
  • '\0' for char (of course this is just the same as 0, and char is an integer type)
  • NULL for pointers.

The Answer 8

3 people think this answer is useful
void function(void) {
  MY_TYPE a;
  a.flag = true;
  a.value = 15;
  a.stuff = 0.123;
}

The Answer 9

2 people think this answer is useful

If MS has not updated to C99, MY_TYPE a = { true,15,0.123 };

The Answer 10

2 people think this answer is useful

I found another way to initialize structs.

The struct:

typedef struct test {
    int num;
    char* str;
} test;

Initialization:

test tt = {
    num: 42,
    str: "nice"
};

As per GCC’s documentation, this syntax is obsolete since GCC 2.5.

The Answer 11

2 people think this answer is useful

I didn’t like any of these answers so I made my own. I don’t know if this is ANSI C or not, it’s just GCC 4.2.1 in it’s default mode. I never can remember the bracketing so I start with a subset of my data and do battle with compiler error messages until it shuts up. Readability is my first priority.

    // in a header:
    typedef unsigned char uchar;

    struct fields {
      uchar num;
      uchar lbl[35];
    };

    // in an actual c file (I have 2 in this case)
    struct fields labels[] = {
      {0,"Package"},
      {1,"Version"},
      {2,"Apport"},
      {3,"Architecture"},
      {4,"Bugs"},
      {5,"Description-md5"},
      {6,"Essential"},
      {7,"Filename"},
      {8,"Ghc-Package"},
      {9,"Gstreamer-Version"},
      {10,"Homepage"},
      {11,"Installed-Size"},
      {12,"MD5sum"},
      {13,"Maintainer"},
      {14,"Modaliases"},
      {15,"Multi-Arch"},
      {16,"Npp-Description"},
      {17,"Npp-File"},
      {18,"Npp-Name"},
      {19,"Origin"}
    };

The data may start life as a tab-delimited file that you search-replace to massage into something else. Yes, this is Debian stuff. So one outside pair of {} (indicating the array), then another pair for each struct inside. With commas between. Putting things in a header isn’t strictly necessary, but I’ve got about 50 items in my struct so I want them in a separate file, both to keep the mess out of my code and so it’s easier to replace.

The Answer 12

2 people think this answer is useful

This can be done in different ways:

MY_TYPE a = { true, 1, 0.1 };

MY_TYPE a = { .stuff = 0.1, .flag = true, .value = 1 }; //designated initializer, not available in c++

MY_TYPE a;
a = (MY_TYPE) { true,  1, 0.1 };

MY_TYPE m (true, 1, 0.1); //works in C++, not available in C

Also, you can define member while declaring structure.

#include <stdio.h>

struct MY_TYPE
{
    int a;
    int b;
}m = {5,6};

int main()
{
    printf("%d  %d\n",m.a,m.b);    
    return 0;
}

The Answer 13

0 people think this answer is useful

Structure in C can be declared and initialized like this:

typedef struct book
{
    char title[10];
    char author[10];
    float price;
} book;

int main() {
    book b1={"DS", "Ajay", 250.0};

    printf("%s \t %s \t %f", b1.title, b1.author, b1.price);

    return 0;
}

The Answer 14

0 people think this answer is useful

I have read the Microsoft Visual Studio 2015 Documentation for Initializing Aggregate Types yet, all forms of initializing with {...} are explained there, but the initializing with dot, named ”designator” isn’t mentioned there. It does not work also.

The C99 standard chapter 6.7.8 Initialization explains the possibility of designators, but in my mind it is not really clear for complex structs. The C99 standard as pdf .

In my mind, it may be better to

  1. Use the = {0};-initialization for all static data. It is less effort for the machine code.
  2. Use macros for initializing, for example

    typedef MyStruct_t{ int x, int a, int b; } MyStruct; define INIT_MyStruct(A,B) { 0, A, B}

The macro can be adapted, its argument list can be independent of changed struct content. It is proper if less elements should be initialized. It is also proper for nested struct. 3. A simple form is: Initialize in a subroutine:

void init_MyStruct(MyStruct* thiz, int a, int b) {
  thiz->a = a; thiz->b = b; }

This routine looks like ObjectOriented in C. Use thiz, not this to compile it with C++ too!

MyStruct data = {0}; //all is zero!
init_MyStruct(&amp;data, 3, 456);

The Answer 15

-2 people think this answer is useful

I’ve been looking for a nice way to initialize my struct, and I’ve got to using the below (C99). This lets me initialize either a single structure or an array of structures in the same way as plain types.

typedef struct {
    char *str;
    size_t len;
    jsmntok_t *tok;
    int tsz;
} jsmn_ts;

#define jsmn_ts_default (jsmn_ts){NULL, 0, NULL, 0}

This can be used in the code as:

jsmn_ts mydata = jsmn_ts_default; /* initialization of a single struct */

jsmn_ts myarray[10] = {jsmn_ts_default, jsmn_ts_default}; /* initialization of
                                                    first 2 structs in the array */

Add a Comment