Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.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_Struct_Printf_Scanf_Unions - Fatal编程技术网

C 结构内部的并集

C 结构内部的并集,c,struct,printf,scanf,unions,C,Struct,Printf,Scanf,Unions,因此,我有以下代码: #include <stdio.h> #include <stdlib.h> struct lista{ union info{ double operando; char operador; }info; }; typedef struct lista Lista; int main(){ printf("char: "); scanf("%c", Lista.info.oper

因此,我有以下代码:

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

 struct lista{ 
    union info{
       double operando;
       char operador;
    }info;
 };

 typedef struct lista Lista;

 int main(){

 printf("char: ");
 scanf("%c", Lista.info.operador);
 getchar();
 printf("%c\n", Lista.info.operador);
 printf("double: ");
 scanf("%lf", Lista.info.operando);
 getchar();
 printf("%lf\n", Lista.info.operando);

 return 0;

 }
#包括
#包括
结构列表{
联盟信息{
双操作数;
歌剧演员;
}信息;
};
类型定义结构lista lista;
int main(){
printf(“char:”);
scanf(“%c”,Lista.info.operador);
getchar();
printf(“%c\n”,Lista.info.operador);
printf(“双:”);
scanf(“%lf”,Lista.info.operation);
getchar();
printf(“%lf\n”,Lista.info.operano);
返回0;
}
当我试图编译它时,我得到以下错误:

错误:“Lista”之前应为表达式


有人知道我做错了什么吗?

您从未声明名为
Lista
Lista
的变量是一种类型,而不是变量,您需要执行以下操作:

Lista xyzzy;
:
scanf ("%c", &(xyzzy.info.operador));
typedef struct { 
    union {
        double operando;
        char operador;
    } info;
} Lista;
您还将注意到,由于
scanf
函数族希望获得变量的地址,以便填充变量,因此我将调用改为使用
&(xyzy.info.operador)
,而不仅仅是
xyzy.info.operador


而且,除非您需要使用结构名称,否则我倾向于只定义类型名称,例如:

Lista xyzzy;
:
scanf ("%c", &(xyzzy.info.operador));
typedef struct { 
    union {
        double operando;
        char operador;
    } info;
} Lista;

在任何特定行上?在出现的所有行上:“Lista.info.operato”或“Lista.info.operator”主要问题是定义一个结构,然后定义一个名为Lista的类型,但实际上并没有在main中创建该类型的对象。您需要处理结构的一个实例。因此,添加类似于
Lista myLista的内容和在main中将
Lista
的所有引用更改为
myLista
myLista
是一个变量名,因此它可以是您选择的名称。另一个考虑因素是您没有正确使用scanf。您需要传递字符变量(或double)的地址,而不是实际的字符或double。因此,您应该执行
scanf(“%c”和&Lista.info.operador)
scanf(“%lf”和&myLista.info.operano)。附带说明
%lf
是一个gcc格式扩展,不可移植。现在只缺少了&。谢谢现在,我在函数“main”中只得到了两个警告:/home/rafael-linux/Documents/ED/union/main.c | |:home/rafael-linux/Documents/ED/union/main.c | 16 |警告:格式“%c”要求参数类型为“char*”,但参数2的类型为“int”[-Wformat=]|/home/rafael linux/Documents/ED/union/main.c | 20 |警告:格式“%lf”需要类型为“double*”的参数,但参数2的类型为“double”[-Wformat=]| | |===构建完成:0个错误、2个警告(0分钟、0秒))====|和一个分段fault@Rafael,这是因为您没有传递变量的地址。请参阅我的更新。@RafaelDias您将发现的其他缺陷。请尝试为
char:
输入两个或更多字符。对于我的问题,我将只为char读取一个字符,但还是要感谢:)