Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot initialize an array of struct within a struct

Tags:

c

I'm trying to create an RPG-esque inventory, my inventory contains a type 'sword' which is an array. Here's the code for my sword struct:

typedef struct
{
    char weaponName[35];
    int damage;
    int rarity;
    float price;

} sword;

Here's the one for the inventory:

typedef struct
{
    sword SwordInvent[size];
} inventory;

I tried to initialize the SwordInvent array in the main function but it ends up with an error.

[Error] expected expression before '{' token

main()
{
    inventory inv;
    inv.SwordInvent[0] = {"Paper Sword", 1, 1, 1};
}

Can anyone be kind enough to help me get out of this hole? :(

EDIT I could not thank you all enough! I wasn't really expecting to get this much of help! Seriously, thank you!

like image 337
Choco Fernandez Avatar asked Sep 29 '16 14:09

Choco Fernandez


People also ask

How do you initialize an array within a struct?

Here is how initialize the structure : struct str { char arr1[4]; // this string is limited to three characters plus null char arr2[4]; int num1[4]; int num2[4]; }; struct str select = { "Sam", "Joe", { 0, 1, 2, 3 }, { 4, 5, 6, 7 } };

Can you initialize inside a struct?

Que: Can we initialize structure members within structure definition? No! We cannot initialize a structure members with its declaration, consider the given code (that is incorrect and compiler generates error).

Do we need to initialize struct in c#?

Struct initialization and default valuesAll of a struct's member fields must be definitely assigned when it's created because struct types directly store their data. The default value of a struct has definitely assigned all fields to 0.

Can an array be declared as a structure member?

An array within a structure is a member of the structure and can be accessed just as we access other elements of the structure.


2 Answers

You can't just start listing stuff inside braces and have the compiler magically figure out what you want. It doesn't work like that, except with initializers. Initializers, e.g.

const sword excalibur = { "Excalibur!", INT_MAX, INT_MAX, FLT_MAX };

are different from assignment. With initializers, it's assumed that the right-hand side is going to match the left-hand side, since it's an initializer. Also, they existed longbefore compound literals, so way back this was all you could to with = and structures.

Your code (assignment) is a bit like this:

float x;
x = 1 / 2;

This does not do a floating-point division, since 1 and 2 are both integers; the compiler does not figure out that this should have a floating point result based on the type of the left-hand side (and no, with an initializer for a float it still doesn't help, float x = 1 / 2; will not assign 0.5 but 0; end of analogy).

The same is true (kind of) for your brace-list assignment. There needs to be something on the right hand side that indicates the type of the braced list.

Thus, you need to use the compound literal syntax:

inv.SwordInvent[0] = (sword) {"Paper Sword",1,1,1};

Also, your sword is not an array, it's a struct.

like image 58
unwind Avatar answered Sep 25 '22 12:09

unwind


When you write:

inventory inv;
inv.SwordInvent[0]={"Paper Sword",1,1,1};

you are not doing initialization; you are doing (attempting to do) an assignment. Initialization would look like:

inventory inv = { { "Paper Sword", 1, 1, 1 } };

Also note that this initializes all elements of the inv variable; the ones without an explicit value are zeroed.

If you want to do assignment, use a compound literal (C99 or later):

inventory inv;
inv.SwordInvent[0] = (sword){ "Paper Sword", 1, 1, 1 };

Note that after this executes, only element 0 of the array has values assigned; all the other elements are still uninitialized (have indeterminate values). So, unless size == 1, there's a fairly big difference between the initialization and the assignment.

like image 27
Jonathan Leffler Avatar answered Sep 22 '22 12:09

Jonathan Leffler