Skip to content

Instantly share code, notes, and snippets.

@jcavat
Created July 16, 2020 07:17
Show Gist options
  • Save jcavat/126d32f8250101e9a3c9fc21d96bba1f to your computer and use it in GitHub Desktop.
Save jcavat/126d32f8250101e9a3c9fc21d96bba1f to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char* id;
int capacity;
} Room;
typedef struct {
char* fullname;
char* numberPhone;
} Professor;
typedef struct {
enum { PROF, ROOM } kind;
union {
Room room;
Professor prof;
} type;
} Resource;
void print(Resource *resource) {
switch( resource->kind ){
case ROOM:
printf("room id: %s with capacity: %i\n",
resource->type.room.id,
resource->type.room.capacity);
break;
case PROF:
printf("professeur fullname: %s, numberphone: %s\n",
resource->type.prof.fullname,
resource->type.prof.numberPhone);
break;
}
}
int main() {
Resource resources[3] = {
{.kind = PROF, .type = {.prof.fullname="Paul",.prof.numberPhone="62433"}},
{.kind = PROF, .type = {.prof.fullname="Oreste",.prof.numberPhone="62422"}},
{.kind = ROOM, .type = {.room.id="A406", .room.capacity=50}}
};
for(int i = 0; i < 3; i++){
print(&resources[i]);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment