Best way to implement struct polymorphism [duplicate] - c

This question already has answers here:
Best approach for struct polymorphism in C [closed]
(3 answers)
easy struct inheritance & pseudo-polymorphism vs strict aliasing
(2 answers)
Is it possible to do inheritance from an abstract/base struct or simulate something along those lines in C?
(3 answers)
Closed 7 years ago.
The problem I'm facing in C is that I'd like to have a series of structs that have a base member from another struct. e.g.
struct foo {
int a;
void (*calculate)(struct foo *);
};
struct bar {
int a;
void (*calculate)(struct foo *);
double b;
};
void do_thing(struct foo *a)
{
a->calculate(a);
}
The problem I'm facing is that the following appears to violate strict aliasing rules.
void foo_calculate(struct foo *a)
{
struct bar *b = (struct bar*)a;
}
The only way I've come up with to do this is to create a union inside struct foo that contains all the structs that inherit from it. Is there an easier way to accomplish this?

Related

Defining a new instance of a struct [duplicate]

This question already has answers here:
Is it optional to use struct keyword before declaring a structure object?
(3 answers)
Closed 2 years ago.
I tried typing the following code in a simple C project, but it keeps saying that MyStruct is undefined – unless I add struct before every MyStruct (i.e. struct MyStruct my_struct; which just feels wrong?).
struct MyStruct {
int my_int;
}
int main() {
MyStruct my_struct;
my_struct->my_int = 1;
return 0;
}
It’s not wrong, it’s the way C works. Type name is struct MyStruct (it would be simply MyStruct in C++). If you feel that inconvenient, make a typedef, like:
typedef struct MyStruct { ... } MyStruct;
That may or may not be considered a good practice, though.
Also note that a struct (but not a typedef) and a function can have the same name (without the struct prefix). sigaction is a real-word example of that.

Question about some type of element in C struct [duplicate]

This question already has answers here:
How do function pointers in C work?
(12 answers)
Closed 4 years ago.
I have some issue understanding some code in C.
Here is an extract :
typedef struct player_s {
point_t (*get_action)(struct player_s *, game_state_t *);
void (*setup_boats)(struct player_s *, game_state_t *);
char *name;
point_t owned_rect[2];
int n_boats;
} player_t;
I don't understand what do the 2 first line of this struct..
They also do this :
local_player_t *ret = calloc(1, sizeof(*ret));
ret->base.get_action = playerLocalAction;
local_player_t is a struct that contain a player_t.
and here is playerLocalAction prototype :
static point_t playerLocalAction(player_t *self, game_state_t *game);
I really don't understand what's appening in this code..
If you can help me thanks !
(Sorry for my bad english, I have to work on it x) )
These are pointers to a function. They are defined as :
[ret_type] (*[ptr_name])([arg_types])
Those function pointers are very useful in many situations, allowing a better modularity.

C structs: Can a function argument be used to deference a struct pointer [duplicate]

This question already has answers here:
How can I access structure fields by name at run time?
(5 answers)
Passing a struct member NAME to function in C?
(1 answer)
How to access members of a `struct' according to a value of a string?
(3 answers)
Closed 5 years ago.
I'm learning about OOP C and I want to write the equivalent of getters and setters.
I have the following struct.
typedef struct {
int item0;
int item1;
int item2;
etc...;
} ItemsStr;
typedef ItemsStr * ItemsPtr;
items_get_item(ItemsPtr item, itemname) {
return item->itemname;
}
I realize that the syntax is invalid but is there a way to accomplish the above?
I also realize that I could write a switch statement but I was wondering if there was a way to use the parameter passed in to deference.
You can use offsetof():
int items_get_item(ItemsPtr items, size_t offset) {
return *(int*)((char*)items + offset);
}
Then the caller does this:
items_get_item(items, offsetof(ItemsStr, item1))

When to use pointers in C [duplicate]

This question already has answers here:
C best practices, stack vs heap allocation
(4 answers)
Closed 6 years ago.
I have a struct like this:
typedef struct {
int hi;
} my_struct;
Is there an advantage in using this:
my_struct *test = malloc(sizeof(my_struct));
test->hi = 1;
Instead of this:
my_struct test;
test.hi = 1;
No, usually it's quite the opposite. If you can use the format to satisfy your requrement
my_struct test;
test.hi = 1;
then it's always better, less overhead in runtime. There's no advantage in general of using memory allocator functions, when you can do without them.

What's the aim of typedef a former name? [duplicate]

This question already has answers here:
typedef struct vs struct definitions [duplicate]
(12 answers)
Closed 10 years ago.
Such as:
typedef struct _cairo_clip cairo_clip_t;
Why not directly use _cairo_clip? See numerous similar definitions in some code.
The idea behind typedef is to let you skip the struct keyword. Unlike C++, C does not let you do this:
struct _cairo_clip {
int a;
float b;
};
_cairo_clip cc; // Not allowed
struct _cairo_clip cc; // Allowed, but requires a keyword
If you tried to directly use _cairo_clip, you would need to call it struct _cairo_clip.
struct _cairo_clip is more verbose than cairo_clip_t.
Using a typedef also provides some abstraction, because it means that cairo_clip_t can be implemented as either a built-in type or as implemented as a struct, without causing a change in the syntax of the client code.

Resources