有没有可能;嵌入;将变量类型的结构转换为另一个结构?(GCC)

有没有可能;嵌入;将变量类型的结构转换为另一个结构?(GCC),gcc,struct,embed,mplab,Gcc,Struct,Embed,Mplab,在C语言中,是否可以在另一个结构中嵌入不同类型的结构 基本上我想做这样的事情 struct A { int n; void *config; } struct AConfig { int a; char *b; } struct BConfig { int a; float b; } const struct A table[] = { { 103, (void*)(struct AConfig){ 1932, "hello" } }, { 438, (void*)(stru

在C语言中,是否可以在另一个结构中嵌入不同类型的结构

基本上我想做这样的事情

struct A { int n; void *config; }

struct AConfig { int a; char *b; }
struct BConfig { int a; float b; }

const struct A table[] = {
    { 103, (void*)(struct AConfig){ 1932, "hello" } },
    { 438, (void*)(struct BConfig){ 14829, 33.4f } }
}

这在C中是可能的,还是我必须单独定义结构?

如果BConfig有一个指向b的浮点指针,它就可以工作了

顺便说一下,您可能需要重构代码以适应C语法

#include <stdio.h>
#include <stdlib.h>

typedef struct { int n; void *config; } Config;

typedef struct { int a; char *b; } AConfig;
typedef struct { int a; float *b; } BConfig;

int main(void) {
        AConfig A;
        BConfig B;
        A.a= 103;
        A.b= "hello";
        B.a= 438;
        B.b=(float *) malloc (sizeof(float));
        *(B.b)= 33.4f;
        const Config table[] = {
                { A.a, (void *) A.b },
                { B.a, (void *) B.b }
        };
        printf("Hi\n");
        return 0;
}
#包括
#包括
typedef结构{int n;void*config;}config;
typedef结构{inta;char*b;}AConfig;
typedef结构{inta;float*b;}BConfig;
内部主(空){
乌头;
B配置B;
A.A=103;
A.b=“你好”;
B.a=438;
B.B=(浮动*)malloc(浮动)的大小;
*(B.B)=33.4f;
常量配置表[]={
{A.A,(void*)A.b},
{B.a,(void*)B.B}
};
printf(“Hi\n”);
返回0;
}

如果BConfig有一个指向b的浮点指针,它就会工作

顺便说一下,您可能需要重构代码以适应C语法

#include <stdio.h>
#include <stdlib.h>

typedef struct { int n; void *config; } Config;

typedef struct { int a; char *b; } AConfig;
typedef struct { int a; float *b; } BConfig;

int main(void) {
        AConfig A;
        BConfig B;
        A.a= 103;
        A.b= "hello";
        B.a= 438;
        B.b=(float *) malloc (sizeof(float));
        *(B.b)= 33.4f;
        const Config table[] = {
                { A.a, (void *) A.b },
                { B.a, (void *) B.b }
        };
        printf("Hi\n");
        return 0;
}
#包括
#包括
typedef结构{int n;void*config;}config;
typedef结构{inta;char*b;}AConfig;
typedef结构{inta;float*b;}BConfig;
内部主(空){
乌头;
B配置B;
A.A=103;
A.b=“你好”;
B.a=438;
B.B=(浮动*)malloc(浮动)的大小;
*(B.B)=33.4f;
常量配置表[]={
{A.A,(void*)A.b},
{B.a,(void*)B.B}
};
printf(“Hi\n”);
返回0;
}

不,它不是那样工作的。每个结构都需要显式存储:

struct A { int n; void *config; };

struct AConfig { int a; char *b; };
struct BConfig { int a; float b; };

struct AConfig ac = { 1932, "hello" };
struct BConfig bc = { 14829, 33.4f };

const struct A table[] = {
    { 103, &ac },
    { 438, &bc }
};
编辑: 另一种可能性是利用
联合
和C99(
-std=C99
)命名的初始值设定项:

enum config_type { CT_INT, CT_FLOAT, CT_STRING };

union config_value {
    int int_value;
    float float_value;
    const char* string_value;
};

struct config {
    enum config_type ctype;
    union config_value cvalue;
};

struct config sys_config[] = {
    { CT_INT, { .int_value = 12 }}, 
    { CT_FLOAT, { .float_value = 3.14f }}, 
    { CT_STRING, { .string_value = "humppa" }}};

void print_config( const struct config* cfg ) {
    switch ( cfg->ctype ) {
        case CT_INT: 
            printf( "%d\n", cfg->cvalue.int_value ); break;      
        case CT_FLOAT:
            printf( "%f\n", cfg->cvalue.float_value ); break;
        case CT_STRING:
            printf( "%s\n", cfg->cvalue.string_value ); break;
        default:
            printf( "unknown config type\n" );
    }       
}

不,不是那样的。每个结构都需要显式存储:

struct A { int n; void *config; };

struct AConfig { int a; char *b; };
struct BConfig { int a; float b; };

struct AConfig ac = { 1932, "hello" };
struct BConfig bc = { 14829, 33.4f };

const struct A table[] = {
    { 103, &ac },
    { 438, &bc }
};
编辑: 另一种可能性是利用
联合
和C99(
-std=C99
)命名的初始值设定项:

enum config_type { CT_INT, CT_FLOAT, CT_STRING };

union config_value {
    int int_value;
    float float_value;
    const char* string_value;
};

struct config {
    enum config_type ctype;
    union config_value cvalue;
};

struct config sys_config[] = {
    { CT_INT, { .int_value = 12 }}, 
    { CT_FLOAT, { .float_value = 3.14f }}, 
    { CT_STRING, { .string_value = "humppa" }}};

void print_config( const struct config* cfg ) {
    switch ( cfg->ctype ) {
        case CT_INT: 
            printf( "%d\n", cfg->cvalue.int_value ); break;      
        case CT_FLOAT:
            printf( "%f\n", cfg->cvalue.float_value ); break;
        case CT_STRING:
            printf( "%s\n", cfg->cvalue.string_value ); break;
        default:
            printf( "unknown config type\n" );
    }       
}

你可能更喜欢工会。我的联合语法有点生疏,但类似于:

union config { char* c; float d; };
struct A {int n; int a; union config b;};

const struct A table[] = {
    {103, 1932, { .c = "hello" } },
    {14829, 438, { .d = 33.4f } }
};

对于指定的初始化器(表中的.c或.d),您需要C99,并且显然需要某种方法来判断您是否正在访问char*或float,但我假设您已经在其他地方介绍了这一点

你可能更喜欢工会。我的联合语法有点生疏,但类似于:

union config { char* c; float d; };
struct A {int n; int a; union config b;};

const struct A table[] = {
    {103, 1932, { .c = "hello" } },
    {14829, 438, { .d = 33.4f } }
};
对于指定的初始化器(表中的.c或.d),您需要C99,并且显然需要某种方法来判断您是否正在访问char*或float,但我假设您已经在其他地方介绍了这一点

您可以使用联合体:

struct AConfig { int a; char *b; };
struct BConfig { int a; float b; };
struct A {
    int n;
    union {
        struct AConfig a;
        struct BConfig b;
    };
};
请注意,
a
b
位于内存中完全相同的空间中。因此,如果要使用
A.A
,则不应使用
A.b
,反之亦然

由于这是一个匿名联合,您可以引用
a
b
,就像它们是结构a的直接字段一样:

struct A sa;
sa.n = 3;
sa.b.a = 4;
sa.b.b = 3.14;
您可以使用工会:

struct AConfig { int a; char *b; };
struct BConfig { int a; float b; };
struct A {
    int n;
    union {
        struct AConfig a;
        struct BConfig b;
    };
};
请注意,
a
b
位于内存中完全相同的空间中。因此,如果要使用
A.A
,则不应使用
A.b
,反之亦然

由于这是一个匿名联合,您可以引用
a
b
,就像它们是结构a的直接字段一样:

struct A sa;
sa.n = 3;
sa.b.a = 4;
sa.b.b = 3.14;