I have a struct like this:
struct foobar {
int i;
char *word;
};
I know this will work:
struct foobar {
int i;
char *word;
};
struct foobar three = {3, "three"};
Why this doesn't work:
struct foobar {
int i;
char *word;
} three;
three = {3, "three"};
It will give an error: expected expression
Thanks!
"Initialization" means to define the initial value of a variable right in the course of variable definition. Assignment, in contrast, assigns a value to a variable defined elsewhere in the program. C does not support assignment of values to variables of type struct or array, but it supports initialization of variables of these types:
C does not support assignment of values to variables of type struct or array, but it supports initialization of variables of these types: struct foobar three = {3, "three"} is an initialization, since the value is defined together with the variable definition. This is supported in C and in C++.
5) In your "initializers", you cannot refer to variable names. The values you supply have to cover every field and be provided in the order that they show up in the structure: 6) When you use the struct keyword, you're defining a new structure. Your struct str select is a very bad attempt at re-defining the str structure.
Your struct str select is a very bad attempt at re-defining the str structure. And that's just for starters... Your code is so bad everywhere, it makes it impossible to try to figure out what you're really trying to do and what the structure should look like.
"initialization" and "assignment", though having quite similar syntax, are two different things with different restrictions.
"Initialization" means to define the initial value of a variable right in the course of variable definition. Assignment, in contrast, assigns a value to a variable defined elsewhere in the program.
C does not support assignment of values to variables of type struct
or array
, but it supports initialization of variables of these types:
struct foobar three = {3, "three"}
is an initialization, since the value is defined together with the variable definition. This is supported in C and in C++.
struct foobar three; three = {3, "three"}
in contrast, is an assignment, because the variable is first declared, but the value is assigned in a separate statement. This is not supported in C, but would be supported in C++.
It looks like you are trying to use the so-called "instantiation syntax" here. Unfortunately, this only works at the very moment you declare the variable!
If you do it after declaration (like in your example), you have to use one of the more cumbersome ways.
To clarify, this is how it would work:
struct foobar {
int i;
char *word;
} three = {3, "three"};
It doesn't work, because C doesn't know what type {3, "three"} should be of; C doesn't look at the left side of the "=" operator to guess your type, so you don't have any type information there. With C99 you can use a compound literal for this:
three = (struct foobar) { 3, "three" };
The cast gives the type, the values in the curly brackets the initiallizer. The result is than assigned to your variable three.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With