Using a designated initializer to initialize nested struct with a `char []` member

喜你入骨 提交于 2020-03-22 08:42:33

问题


I have the following nested struct definition:

typedef struct {
    int count;
    float cash;
    char item[50];//switch between array and pointer for testing initializers
    //char *item;
}Purchase;

typedef struct {
    int accnt;
    char acct_name[50];
    Purchase purch;
} Acct;

Where for the Purchase struct by itself, the following initializer works:

//Uses member names:
/* 1 */Purchase p = {.count = 4, .cash = 12.56, .item = "thing"};
//  Note: this member:                          ^^^^^^^^^^^^^^^

And for the nested struct Acct the following works:

// No member names:
/* 2 */Acct acct = {100123, "Robert Baily", {15, 12.50, "Tires"}};
//                                                      ^^^^^^^

but when I attempt to use the member name, as in the first example:

// Attempts to use member name, but fails the last one:
/* 3 */Acct acct3 = {.accnt = 100123, .acct_name = "Robert Baily", {acct3.purch.count = 15, acct3.purch.cash = 12.50, acct3.purch.item = "Tires"}};
// error occurs here ->                                                                                                                ^

I get this error: 22, 131 error: array type 'char [50]' is not assignable when using member char item[50]; inPurchase`

And I get this error: 22, 14 error: initializer element is not a compile-time constant When using member char *item; in Purchase (Note only one version of item is part of the struct at any one time, the other is commented)

So, in summary, I can initialize a nested struct okay if not using named assignment statements as in statement /* 2 */ above, but when I attempt to use named assignments as shown for char [] types in statement /* 3 */, it fails.

What am I missing for initializing a char [] or char * when it is a member of the inner struct of a nested struct construct?

I am using the CLANG set to C99


回答1:


You are trying to compile:

Acct acct3 = {.accnt = 100123, .acct_name = "Robert Baily", 
           {acct3.purch.count = 15, acct3.purch.cash = 12.50, acct3.purch.item = "Tires"}};

This initializer is not valid because acct3.purch is not a field name of Purchase. You only use the innermost field names when initializing.

Acct acct3 = {.accnt = 100123, .acct_name = "Robert Baily", 
              .purch = {.count = 15, .cash = 12.50, .item = "Tires"}};



回答2:


Neither structure Acct nor structure Purchase have data member name acct3. So this initializer list for the variable Acct acct3

{acct3.purch.count = 15, acct3.purch.cash = 12.50, acct3.purch.item = "Tires"}

is invalid.

Taking into account the error message the compiler considers such a part of the initializer list like this

acct3.purch.item = "Tires"

as an assignment of the data member item of the object acct3 and arrays do not have the assignment operator because they are non-modifiable lvalues.



来源:https://stackoverflow.com/questions/60746158/using-a-designated-initializer-to-initialize-nested-struct-with-a-char-memb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!