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

前端 未结 15 2690
予麋鹿
予麋鹿 2020-11-21 22:59

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;         


        
相关标签:
15条回答
  • 2020-11-21 23:48

    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;
    }
    
    0 讨论(0)
  • 2020-11-21 23:52

    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!

    0 讨论(0)
  • 2020-11-21 23:57

    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 };
    
    0 讨论(0)
  • 2020-11-22 00:01

    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.
    0 讨论(0)
  • 2020-11-22 00:01

    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(&data, 3, 456);
    
    0 讨论(0)
  • 2020-11-22 00:04
    void function(void) {
      MY_TYPE a;
      a.flag = true;
      a.value = 15;
      a.stuff = 0.123;
    }
    
    0 讨论(0)
提交回复
热议问题