Initializing a Global Struct in C
Posted
by Scott
on Stack Overflow
See other posts from Stack Overflow
or by Scott
Published on 2010-03-26T08:35:02Z
Indexed on
2010/03/26
8:43 UTC
Read the original article
Hit count: 785
What is the best way to accomplish the following in C?
#include <stdio.h>
struct A
{
int x;
};
struct A createA(int x)
{
struct A a;
a.x = x;
return a;
}
struct A a = createA(42);
int main(int argc, char** argv)
{
printf("%d\n", a.x);
return 0;
}
When I try to compile the above code, the compiler reports the following error:
"initializer element is not constant"
The bad line is this one:
struct A a = createA(42);
Can someone explain what is wrong? I'm not very experienced in C. Thanks!
© Stack Overflow or respective owner