简体   繁体   English

访问其联合内的结构的字段

[英]Access a field of a structure that is within the union of it

This program must handle data that represents the products used in the factory. 该程序必须处理代表工厂使用的产品的数据。 It is desired that the product represents the following characteristics: Code, description unit of measure, and price; 希望该产品具有以下特征:代码,度量单位描述和价格; In turn, if the product is imported you want to know the origin of the product, while if it is purchased in the square you want to know the name of the provider's phone number. 反过来,如果产品是进口的,则您想知道产品的来源,而如果是在广场上购买的,则要知道提供商的电话号码的名称。

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <stdbool.h>
#include <winsock.h>

void clrscr();

typedef enum {IMPORTED, LOCAL} type;

typedef struct
{
    int code;
    char description [20];
    char MeasureUnit [5];
    float price;
    type discriminant;
    union
    {
        char origin [20];
        char destination [20];
        int telephone;
    } impoExpo;
} Product;

//this procedure fails
void loadProduct (Product *p)
{
    printf("\nEnter the code:");
    scanf("%d",&Product.code); //<-error: expected expression before 'Product'
    fflush(stdin);
    printf("\nEnter the description:");
    scanf("%s",Product.description);
    printf("Indicate the unit of measure:");
    scanf("%s",Product.MeasureUnit);
    printf("Enter the price:");
    scanf("%f",&Product.price);
    int i;
    printf("\nInsert 1 if imported");
    scanf("%d", &i);
    if(i == 1)
    {
        p->discriminant = IMPORTED;
    }
    else
    {
        p->discriminant = LOCAL;
    }
    if(p->discriminant == IMPORTED)
    {
        printf("\nEnter source: ");
        gets(p->impoExpo.origin);
    }
    else
    {
        printf("\nEnter the phone");
        scanf("%d", &p->impoExpo.telephone);
    }
}

//it is also
void showProduct (Product p)
{
    printf("\nCode: %d", p.code); //<----- error: request for member 'code' in something not a structure or union
    printf("\nDescription");
    printf("%s", p.description);
    printf("\nMeasurement unit:");
    printf("%s", p.MeasureUnit);
    printf("\nPrice:% .2f", p.price);
    printf("\nType:");
    if (p.discriminant == IMPORTED)
    {
        printf("Imported:");
        printf("\nOrigin: %s", p.impoExpo.origin);
        printf("%s", p.impoExpo.origin);
    }
    else
    {
        printf("Local:");
        printf("\nTelephone: %d", p.impoExpo.telephone);
    }
}

//this one also
bool areequal (Product p1, Product p2)
{
    bool equal = false;
    if ((p1.code == p2.code) && (p1.description == p2.description))
    {
        if ((p1.MeasureUnit == p2.MeasureUnit) && (p1.price == p2.price))
        {
            if (p1.discriminant == p2.discriminant)
            {
                if (p1.discriminant == IMPORTED)
                {
                    if (p1.impoExpo.origin == p2.impoExpo.origin)
                    {
                        equal = true;
                    }
                }
                else
                {
                    if (p1.impoExpo.telephone == p2.impoExpo.telephone)
                    {
                        equal = true;
                    }
                }
            }
        }
    }
    return equal;
}

//this función ok
void copy (Product * const destination, const Product * const origin)
{
    destination->code = origin->code;
    (*destination->description) = (*origin->description);
    (*destination->MeasureUnit) = (*origin->MeasureUnit);
    destination->price = origin->price;
    destination->discriminant = origin->discriminant;

    if(destination->discriminant == IMPORTED)
        (*destination->impoExpo.origin) = (*origin->impoExpo.origin);
    else
        destination->impoExpo.telephone = origin->impoExpo.telephone;
}

//and the latter also
int main ()
{
    int option;
    do
    {
        clrscr();
        printf("Welcome to the program\n");
        printf("Enter an option\n");
        printf("1. Load a product\n");
        printf("2. Show product\n");
        printf("3. Check if two products are the same\n");
        printf("0. Exit");
        printf("Enter the option, and press ENTER");
        scanf("%d",&option);

        switch (option)
        {
        case 1:
            loadProduct(&p);
            getch();
            break;
        case 2:
            showProduct(p);
            getch();
            break;
        case 3:
            printf("Enter the name of the product 1");
            scanf("%d",&p1);
            printf("Enter the name of the product 2");
            scanf("%d",&p2);
            printf("% d",areequal (p1, p2));
            getch();
            break;
        }
    } while (option != 0);
    getch();
    return 0;
}

void clrscr()
{
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coord = {0, 0};
    DWORD count;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(hStdOut, &csbi);
    FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
    SetConsoleCursorPosition(hStdOut, coord);
}

Product是一个指针,因此您必须使用箭头符号访问其成员:

    scanf("%d",&p->code); //<-error: expected expression before 'Product'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM