Reputation: 93
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!
Upvotes: 7
Views: 658
Reputation: 131
You have to specify the type before assignment:
inv.SwordInvent[0] = (sword) { "Paper Sword", 1, 1, 1 };
Upvotes: 2
Reputation: 399803
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.
Upvotes: 5
Reputation: 8209
inv.SwordInvent[0]={"Paper Sword",1,1,1};
Such assignment is illegal. Expression at the right side of =
should have proper type. The easiest way is to use compound literal (as mentioned in other answers):
inv.SwordInvent[0] = (sword){"Paper Sword",1,1,1};
Alternatively you can use next initialization:
inventory inv = {
{
{"Paper Sword",1,1,1}, /* init SwordInvent[0] */
{"Paper Sword",1,1,1}, /* init SwordInvent[1] */
/* ... */
}
};
This variant conforms to C89, that may be useful if your compiler doesn't support newest language features.
Upvotes: 1
Reputation: 753665
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.
Upvotes: 5
Reputation: 30136
You can do it at the declaration line.
The simplest way to emulate what you're trying to do is:
inventory inv = {{"Paper Sword",1,1,1}};
A more general (and perhaps readable) manner would be:
inventory inv =
{
.SwordInvent =
{
{
.weaponName = "Paper Sword",
.damage = 1,
.rarity = 1,
.price = 1
}
}
};
If you want to initialize, say, two entries in the array, then you could use something like:
inventory inv =
{
.SwordInvent =
{
{
.weaponName = "Paper Sword",
.damage = 1,
.rarity = 1,
.price = 1
},
{
.weaponName = "Light Saber",
.damage = 100,
.rarity = 100,
.price = 100
}
}
};
Upvotes: 1