【发布时间】:2020-02-03 12:30:28
【问题描述】:
我是新来的,希望能得到一些帮助。
在做了一个关于structs和unions的教程之后,我实践了我所学的,并且想结合两个想法。但是我遇到了一个错误,我已经尝试解决了几个小时,但没有任何运气。
代码是:
void main(){
typedef enum {INDIV, OUNCE, POUND } quantity;
typedef union{
short individual;
float pound;
float ounce;
} amount;
typedef struct{
char *brand;
amount theAmount;
} orangeProduct;
orangeProduct productOrdered;
quantity quantityType = OUNCE;
switch(quantityType)
{
case INDIV:
productOrdered = {"Chiquita", .theAmount.individual = 13 };
printf("You bought %d oranges\n\n", productOrdered.theAmount.individual)
break;
}
}
我也有其他情况,但这没关系:如果我解决了第一个问题,我将解决所有问题。 目标是拥有可以容纳橙子品牌的结构,并为每个品牌容纳购买的数量、正确的重量类型。
问题出在这行:
productOrdered = {"Chiquita", productOrdered.theAmount.individual = 13 };
我的gcc compiiler(使用codeBlocks)对我大喊:
错误:“{”标记之前的预期表达式|
而且,无论我怎么尝试,都无济于事。
请帮忙。
【问题讨论】: