Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to initialize a const struct without using a function?

Tags:

c

I have a fairly simple const struct in some C code that simply holds a few pointers and would like to initialize it statically if possible. Can I and, if so, how?

like image 507
quikchange Avatar asked Oct 13 '08 20:10

quikchange


People also ask

Can a struct have const member?

No, a struct is a class where members and bases are public by default. Structs can still have private members.

Is it necessary to initialize const variables?

In C++ the const keyword has been improved and you can declare real const values. That's why it has to be initialized before compiling the code. The rule is slightly different: to be used in an integral constant expression, the initialization must be visible to the compiler.


1 Answers

You can, if the pointers point to global objects:

// In global scope
int x, y;
const struct {int *px, *py; } s = {&x, &y};
like image 118
Lev Avatar answered Oct 07 '22 05:10

Lev