Skip to content

Instantly share code, notes, and snippets.

@jcavat
Created July 16, 2020 07:17
Show Gist options
  • Select an option

  • Save jcavat/126d32f8250101e9a3c9fc21d96bba1f to your computer and use it in GitHub Desktop.

Select an option

Save jcavat/126d32f8250101e9a3c9fc21d96bba1f to your computer and use it in GitHub Desktop.

Revisions

  1. jcavat created this gist Jul 16, 2020.
    47 changes: 47 additions & 0 deletions resource.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #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;
    }