File: offsetof1.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 (27 lines) | stat: -rw-r--r-- 614 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
#include <stdio.h>
#include <stddef.h>

typedef void (*FuncPtr)(void);

typedef struct {
    int a;
    FuncPtr func;
    double b;
} MyStruct;

void myFunction(void) {
    printf("Function called!\n");
}

int main() {
    printf("Offset of a: %zu\n", offsetof(MyStruct, a));    // Should be 0
    printf("Offset of func: %zu\n", offsetof(MyStruct, func)); // Should be sizeof(int)
    printf("Offset of b: %zu\n", offsetof(MyStruct, b));    // Should be sizeof(int) + sizeof(FuncPtr)

    // Example usage
    MyStruct s;
    s.func = myFunction;
    s.func(); // Should print "Function called!"

    return 0;
}