@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

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.8133624920378171","path":"main.c","file":{"path":"main.c","content":{"asEncoding":{"base64":"I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnR5cGVkZWYgc3RydWN0IHsKICAgIGludCB4OwogICAgaW50IHk7Cn0gUG9pbnQ7Cgp0eXBlZGVmIHN0cnVjdCB7CiAgICBpbnQgd2lkdGg7CiAgICBpbnQgaGVpZ2h0OwogICAgY2hhciogdGl0bGU7Cn0gQ29uZmlnOwoKdHlwZWRlZiBzdHJ1Y3QgewogICAgQ29uZmlnIGNvbmZpZzsKfSBHYW1lOwoKR2FtZSogY3JlYXRlR2FtZShDb25maWcgY29uZmlnKSB7CiAgICBwdXRzKCJDcmVhdGluZyBnYW1lLi4uIik7CiAgICBHYW1lKiBnYW1lID0gbWFsbG9jKHNpemVvZihHYW1lKSk7CiAgICBnYW1lLT5jb25maWcgPSBjb25maWc7CiAgICByZXR1cm4gZ2FtZTsKfQoKaW50IHJ1bkdhbWUoR2FtZSogZ2FtZSkgewogICAgcHJpbnRmKCJSdW5uaW5nIGdhbWUgKCVkLCAlZClcbiIsIGdhbWUtPmNvbmZpZy53aWR0aCwgZ2FtZS0+Y29uZmlnLmhlaWdodCk7CiAgICByZXR1cm4gMDsKfQoKUG9pbnQgc3VtUG9pbnRzKFBvaW50IGEsIFBvaW50IGIpIHsKICAgIFBvaW50IGM7CiAgICBjLnggPSBhLnggKyBiLng7CiAgICBjLnkgPSBhLnkgKyBiLnk7CiAgICByZXR1cm4gYzsKfQoKUG9pbnQgc3VtUG9pbnRQb2ludGVycyhQb2ludCogYSwgUG9pbnQqIGIpIHsKICAgIFBvaW50IGM7CiAgICBjLnggPSBhLT54ICsgYi0+eDsKICAgIGMueSA9IGEtPnkgKyBiLT55OwogICAgcmV0dXJuIGM7Cn0KCnZvaWQgc29tZUZ1bmN0aW9uKGludCogeCkgeyBwcmludGYoIkhlbGxvISAlZFxuIiwgKngpOyB9CgppbnQgbWFpbigpIHsKICAgIFBvaW50IGEgPSB7MSwgMn07CiAgICBQb2ludCBiID0gezMsIDR9OwogICAgUG9pbnQgYyA9IHN1bVBvaW50cyhhLCBiKTsKCiAgICBQb2ludCBkID0gc3VtUG9pbnRzKChQb2ludCl7NSwgNn0sIChQb2ludCl7NywgOH0pOwogICAgUG9pbnQgZSA9IHN1bVBvaW50UG9pbnRlcnMoJihQb2ludCl7MSwgMn0sICYoUG9pbnQpezMsIDR9KTsKCiAgICBzb21lRnVuY3Rpb24oKGludFtdKXsxLCAyLCAzLCA0LCA1fSk7CgogICAgcHJpbnRmKCJjIC0+IHsgJWQsICVkIH1cbiIsIGMueCwgYy55KTsKICAgIHByaW50ZigiZCAtPiB7ICVkLCAlZCB9XG4iLCBkLngsIGQueSk7CiAgICBwcmludGYoImUgLT4geyAlZCwgJWQgfVxuIiwgZS54LCBlLnkpOwoKICAgIEdhbWUqIGdhbWUgPSBjcmVhdGVHYW1lKChDb25maWcpIHsKICAgICAgICAudGl0bGUgPSAiTXkgQXdlc29tZSBHYW1lIiwKICAgICAgICAud2lkdGggPSA2NDAsCiAgICAgICAgLmhlaWdodCA9IDQ4MAogICAgfSk7CgogICAgcmV0dXJuIHJ1bkdhbWUoZ2FtZSk7Cn0="},"asBuffer":null},"loaded":true}}
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);
}