Struct Embedding in C
C supports structures to group related data together, but it doesn’t have built-in support for embedding or composition like some object-oriented languages. However, we can simulate similar behavior using pointers and function pointers. Here’s an example that demonstrates a similar concept:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define a base structure
typedef struct {
int num;
char* (*describe)(void*);
} Base;
// Function to describe the base
char* base_describe(void* b) {
Base* base = (Base*)b;
char* result = malloc(50);
snprintf(result, 50, "base with num=%d", base->num);
return result;
}
// Define a container structure
typedef struct {
Base base;
char* str;
} Container;
int main() {
// Create a container instance
Container co;
co.base.num = 1;
co.base.describe = base_describe;
co.str = "some name";
// Access the base's fields directly on co
printf("co={num: %d, str: %s}\n", co.base.num, co.str);
// We can also access the full path using the embedded type name
printf("also num: %d\n", co.base.num);
// Call the describe method
char* description = co.base.describe(&co.base);
printf("describe: %s\n", description);
free(description);
// In C, we don't have interfaces, but we can use function pointers
// to achieve similar behavior
char* (*describer)(void*) = co.base.describe;
char* desc = describer(&co.base);
printf("describer: %s\n", desc);
free(desc);
return 0;
}
In this C version:
We define a
Base
structure with an integernum
and a function pointerdescribe
.The
Container
structure includes aBase
as its first member, simulating embedding.We create a
base_describe
function that returns a dynamically allocated string describing the base.In the
main
function, we create aContainer
instance and initialize its members.We can access the base’s fields directly on the container instance.
We demonstrate calling the
describe
method both directly and through a function pointer.Note that in C, we need to manage memory manually, so we free the dynamically allocated strings.
To compile and run this program:
$ gcc -o struct_composition struct_composition.c
$ ./struct_composition
co={num: 1, str: some name}
also num: 1
describe: base with num=1
describer: base with num=1
This example demonstrates how to achieve a similar effect to struct embedding in C, albeit with more manual work and without the syntactic sugar provided by some other languages. It’s important to note that this approach requires careful memory management and doesn’t provide the same level of type safety as languages with native support for these concepts.