Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C有不同的数据类型吗?_C - Fatal编程技术网

C有不同的数据类型吗?

C有不同的数据类型吗?,c,C,如何修复以下程序的c编译器错误 struct a{ int a; }; struct b{ int b; }; int main(){ int toggle =1; int y = (toggle==1) && (struct a x); y = (toggle==0) && (struct b x); if(toggle==1){ x.a = 10; printf("%d ",x.a); }else { x

如何修复以下程序的c编译器错误

struct a{
      int a;
};
struct b{
    int b;
};
int main(){

int toggle =1;
int y = (toggle==1) && (struct a x);
y =     (toggle==0) && (struct b x);

if(toggle==1){
    x.a = 10;
    printf("%d ",x.a);
}else {
    x.b = 20;
    printf("%d ",x.b);
}
printf("hi");
return 0;
}
当我编译这个程序时,在“x”之前出现了错误“expected”)


我需要创建静态对象。有没有其他方法可以做到这一点

不能将声明作为表达式的一部分。您需要找出另一种处理条件编译/声明的方法(可能使用预处理器?)


一种可能的方法是使用公共基础结构,其中的“toggle”作为标志,并使用指向该基础结构类型的指针强制转换为正确的结构。可以说,在C语言中是一种“固有”的软语言。差不多

enum Type
{
    TYPE_A,
    TYPE_B
};

struct Base
{
    int type;  /* One of the Type enumerations */
};

struct A
{
    struct Base base;
    int field_unique_to_a;
};

struct B
{
    struct Base base;
    double field_unique_to_b;
};

int main(void)
{
    int toggle = 1;
    struct Base *base_ptr;

    if (toggle == 1)
    {
        base_ptr = calloc(1, sizeof(A));  /* Use calloc to initialize the data */
        base_ptr->type = TYPE_A;
    }
    else
    {
        base_ptr = calloc(1, sizeof(B));  /* Use calloc to initialize the data */
        base_ptr->type = TYPE_B;
    }

    /* Now `base_ptr` points either to a `A` or a `B` structure */

    if (base_ptr->type == TYPE_A)
    {
        ((struct A *) base_ptr)->field_unique_to_a = 1;
    }
    else
    {
        ((struct B *) base_ptr)->field_unique_to_b = 12.34;
    }

    /* ... */
}

什么是
x
????事实上,
inty=(toggle==1)和&(structax);y=(切换==0)和&(结构b x);xt平均值?