File: postgres_struct_func3.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 (50 lines) | stat: -rw-r--r-- 1,703 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
47
48
49
50

#include <string.h>
#include "test.h"

// Define the functions with the required signatures
void pgstat_archiver_init_shmem_cb(void *stats) {
    printf("Archiver init_shmem_cb called with %p\n", stats);
    ASSERT(0x1234, stats);
}

// Define a struct with two function pointers
struct MyStruct {
    void (*copy_func)(void *dest, const void *src, size_t n); // Function like memcpy
    void (*init_shmem_cb)(void *stats);                       // Function for initializing shared memory
};

// Initialize a static const array of MyStruct with two elements
static const struct MyStruct myArray[2] = {
    [0] = {
        .copy_func = memcpy, // Only initialize the first function
        // init_shmem_cb is not initialized
    },
    [1] = {
        // copy_func is not initialized
        .init_shmem_cb = pgstat_archiver_init_shmem_cb, // Only initialize the second function
    }
};

int main() {
    char src[] = "Hello, world!";
    char dest[50];
    void *dummyStats = (void *)0x1234;  // Some dummy stats pointer

    // Loop through the array and call the appropriate function
    for (int i = 0; i < 2; i++) {
        if (myArray[i].copy_func) {
            // Call the first function if it's initialized
            myArray[i].copy_func(dest, src, sizeof(src));
            printf("Array element %d - Copy result: %s\n", i + 1, dest);
        } else if (myArray[i].init_shmem_cb) {
            // Call the second function if it's initialized
            myArray[i].init_shmem_cb(dummyStats);
            printf("Array element %d - Called init_shmem_cb\n", i + 1);
        } else {
            printf("Array element %d - No function initialized\n", i + 1);
        }
    }

    return 0;
}