简体   繁体   中英

why can't you initialize values of structs outside of main() in c?

I get an error in visual studio with the code:

struct coordinates {
    int x;
    int y;
};
struct coordinates point;
point.x = 5;
point.y = 3;

int main() {
    return 0;
}

it works if I initialize point.x and point.y in main() and / or if I give the point values like this: struct coordinates point = {5, 3} . Why can't you initialize point.x and point.y outside of main() ?

The form you've written is not initialization ; it's an uninitialized (or rather default-initialized, to zero) declaration and definition followed by assignment statements outside of a scope where they're valid.

If you want to initialize the members by name, write:

struct coordinates point = {
    .x = 5,
    .y = 3,
};

Other answers are (correctly) explaining that there are ways to initialize structs by named member outside main statically but I want to add, for the OP or for others.

The basic idea here is that C doesn't allow executable statements ("code") outside of functions. A new C programmer may be coming from another language where code is sort of frequently executed in a "global scope" and you define functions or set variables here and there as you wish. C is way older and much more structured than this. There are global variables (like your point ), which are things you declare outside of any particular function, but you can't really "do stuff" with those variables in the global space itself, except indicate their initial values (static initialization).

C doesn't "run through the file executing everything it finds in order"-- each file gets compiled into a unit of object code which then gets linked together with other units and main is (essentially) the entry point for execution, which in turn calls other functions, and so on-- when your code is running, its always "inside" of a function somewhere.

It is true that ordering of declarations and definitions within a file matter in C, but this is essentially for the convenience of the compiler, not because things are "executed" or "evaluated" in that order at runtime. (By the time your code is executing, it's been utterly transformed into something else and the order of statements in the original files has essentially disappeared from view.)

So: With all that said. it is useful and often desirable to have the values of global variables pre-set at program initialization time. So static initialization is sort of special in that it sorta looks like an executable statement but isn't, and thus it has traditionally had a goofy syntax. We had that sort of strange ordered = {2, 3} syntax, and now there are more accommodations for the named members to help you accomplish this static initialization. But you should still think of it as static (one-time, fixed) initialization, not as executing an assignment in the global space, because that isn't what you're actually doing.

Why can't you initialize point.x and point.y outside of main()?

You can, using static initialization - but you can't call a function or perform impure operations. When using static-initialization prior to C99 you need to ensure fields are set in the correct order, while C99 and later allow for designated initializers that allow arbitrary field initialization.

C code outside of a function are not executable instructions - they're static declarations, consequently they don't have a defined evaluation order.

As a thought-experiment, consider this:

int x;
int y;

x = 2;
y = 3;
y = x;
x = y;

int main() {
    
    return x; // what is returned?
}

Maybe you learned Javascript but you are a newbie in c/c++. Every c/c++ program starts with the main function, and every executive code must be in a function.

So if you want to initialize the value of a struct, you should do this in main function.

struct coordinates {
    int x;
    int y;
};
struct coordinate point;
int main(){
  point.x = 5;
  point.y = 3;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM