Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

Structur Pointer C

I know I for sure misunderstand how to use pointers again. So here is my code. Would be nice if you all can help me. The program is simple. You write values in a structure array and print them out. Even so it would be nice if someone could explain to me when to use double pointers and how to use them probably.

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

#define MAXA 3

typedef enum{
    FOOD,
    ART,
    OTHERS
}TKindOfArticle;

typedef struct{
    int number;
    char description[31+1];
    int sellingGrossPrice;
    int vat;
    int minimumStockLevel;
    TKindOfArticle kindOf;
}TArticle;

void readOneArticle(TArticle* arti);
int readMaxArticle(TArticle* arti[]);
void printfOneArticle(TArticle arti);
void printfMaxArticle(TArticle *arti[],int read);

int main()
{
    TArticle arti[MAXA];
    int howMany;
    howMany = readMaxArticle(&arti);
    printfMaxArticle(&arti,howMany);


    return 0;
}

void readOneArticle(TArticle* arti){
    printf("Number: ");
    scanf("%d", &(arti->number));
    printf("Descrip: ");
    scanf("%s", &(arti->description));
    printf("SellGrossPrice: ");
    scanf("%d",&(arti->sellingGrossPrice));
    printf("MinimumStock: ");
    scanf("%d",&(arti->minimumStockLevel));
    printf("Kind of article (0: Food, 1: Art, 2: Others): ");
    scanf("%d",&(arti->kindOf));

    if(arti->kindOf == FOOD){
        arti->vat= arti->sellingGrossPrice*1.1;
    } else if(arti->kindOf == ART){
        arti->vat= arti->sellingGrossPrice*1.13;
    }else if(arti->kindOf == OTHERS){
        arti->vat= arti->sellingGrossPrice*1.2;
    }
}

int readMaxArticle(TArticle* arti[]){
    int read;
    int i=0;

    printf("Max Elements (max. 3): ");
    scanf("%d",&read);
    
    if(read>MAXA){
        printf("Error");
    } else{
        for(i=0; i<read;i++){
            readOneArticle(arti[i]);
            printf("
");
            printf("Number: %d
Descrip.: %s
Sell Gross: %d
Vat: %d
Min. Stock: %d
",
           (*arti[i]).number,(*arti[i]).description,
           (*arti[i]).sellingGrossPrice,(*arti[i]).vat,(*arti[i]).minimumStockLevel);
        }

    }
    return read;
}

void printfOneArticle(TArticle arti){
    printf("Number: %d
Descrip.: %s
Sell Gross: %d
Vat: %d
Min. Stock: %d
",
           arti.number,arti.description,
           arti.sellingGrossPrice,arti.vat,arti.minimumStockLevel);

    switch(arti.kindOf){
        case 0: printf("Kind: Food
");
                break;
        case 1: printf("Kind: Art
");
                break;
        case 2: printf("Kind: Others
");
                break;
    }
}

void printfMaxArticle(TArticle *arti[],int read){
    if(read>MAXA){
    } else{
        for(int i=0; i<read;i++){
            printfOneArticle(*arti[i]);
            printf("
");
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are creating an array of structures: TArticle arti[MAXA]; But when you call readMaxArticle(&arti) function you pass pointer to array of TArticle. TArticle *arti[] it reads like this - arti is array of pointers to TArticle. But you want to pass pointer to array of TArticle. It won't compile anyway.

error: cannot convert 'TArticle (*)[3]' to 'TArticle**'

You might want to look at how to read C declaration and if you want to practice visit this site.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...