File: offsetof2.c

package info (click to toggle)
chibicc 1.0.23.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,832 kB
  • sloc: ansic: 62,911; sh: 275; makefile: 92
file content (46 lines) | stat: -rw-r--r-- 1,202 bytes parent folder | download
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

#include <stddef.h>
#include "test.h"
typedef void (*Func1)(void);
typedef void (*Func2)(int);
typedef int (*Func3)(int);

typedef struct {
    Func1 func1;
    Func2 func2;
    Func3 func3;
} FunctionStruct;

void myFunc1(void) {
    printf("Func1 called!\n");
}

void myFunc2(int x) {
    printf("Func2 called with %d!\n", x);
}

int myFunc3(int x) {
    return x * 2;
}

int main() {
    printf("Offset of func1: %zu\n", offsetof(FunctionStruct, func1)); // Should be 0
    ASSERT(0, offsetof(FunctionStruct, func1));
    printf("Offset of func2: %zu\n", offsetof(FunctionStruct, func2)); // Should be sizeof(Func1)
    ASSERT(8, offsetof(FunctionStruct, func2) );
    printf("Offset of func3: %zu\n", offsetof(FunctionStruct, func3)); // Should be sizeof(Func1) + sizeof(Func2)
    ASSERT(16, offsetof(FunctionStruct, func3));

    // Example usage
    FunctionStruct fs;
    fs.func1 = myFunc1;
    fs.func2 = myFunc2;
    fs.func3 = myFunc3;

    fs.func1();           // Should print "Func1 called!"
    fs.func2(10);         // Should print "Func2 called with 10!"
    printf("Func3 result: %d\n", fs.func3(5)); // Should print "Func3 result: 10"
    ASSERT(10, fs.func3(5));

    return 0;
}