I was just wondering if a variable declared and defined inside a structure can be initialized to a certain value, was planning on using function pointers to mimic the classes in OOP.
Example COde:
typedef struct{
int x;
int (*manipulateX)(int) = &manipulateX;
}x = {0};
void main()
{
getch();
}
int manipulateX(int x)
{
x = x + 1;
return x;
}
Starting with C99, you can use designated initializers to set fields of structures to values, as follows:
struct MyStruct {
int x;
float f;
};
void test() {
struct MyStruct s = {.x=123, .f=456.789};
}
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