repl.it
@wldomiciano/

compoundListeralDemonstration

C

Demonstra o uso dos Compound Literals para o artigo: https://wldomiciano.com/o-que-sao-compound-literals-em-c/

fork
loading
Files
  • main.c
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int x;
    int y;
} Point;

typedef struct {
    int width;
    int height;
    char* title;
} Config;

typedef struct {
    Config config;
} Game;

Game* createGame(Config config) {
    puts("Creating game...");
    Game* game = malloc(sizeof(Game));
    game->config = config;
    return game;
}

int runGame(Game* game) {
    printf("Running game (%d, %d)\n", game->config.width, game->config.height);
    return 0;
}

Point sumPoints(Point a, Point b) {
    Point c;
    c.x = a.x + b.x;
    c.y = a.y + b.y;
    return c;
}

Point sumPointPointers(Point* a, Point* b) {
    Point c;
    c.x = a->x + b->x;
    c.y = a->y + b->y;
    return c;
}

void someFunction(int* x) { printf("Hello! %d\n", *x); }

int main() {
    Point a = {1, 2};
    Point b = {3, 4};
    Point c = sumPoints(a, b);

    Point d = sumPoints((Point){5, 6}, (Point){7, 8});
    Point e = sumPointPointers(&(Point){1, 2}, &(Point){3, 4});

    someFunction((int[]){1, 2, 3, 4, 5});

    printf("c -> { %d, %d }\n", c.x, c.y);
    printf("d -> { %d, %d }\n", d.x, d.y);
    printf("e -> { %d, %d }\n", e.x, e.y);

    Game* game = createGame((Config) {
        .title = "My Awesome Game",
        .width = 640,
        .height = 480
    });

    return runGame(game);
}