Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struct initialization in C with error: expected expression

Tags:

c

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!

like image 401
JiangIsAtWar Avatar asked Apr 26 '17 19:04

JiangIsAtWar


People also ask

What is the difference between initialization and assignment in C programming?

"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:

Is it possible to assign a variable to a struct in C?

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++.

Can't refer to variable names in initializers?

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.

Why is my struct STR select so bad?

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.


3 Answers

"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++.

like image 32
Stephan Lechner Avatar answered Sep 20 '22 13:09

Stephan Lechner


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"};
like image 27
Rautermann Avatar answered Sep 19 '22 13:09

Rautermann


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.

like image 159
Sascha Avatar answered Sep 20 '22 13:09

Sascha