ページ 11

構造体について

Posted: 2010年1月11日(月) 18:36
by くりく
いまC言語の基本を勉強しているのですが、
構造体でよくわからないところが出たので質問させてもらいます;;
次のプログラムがあったのですが、6行目のところで

struct Goods *foo(struct Goods gs[/url]){

とありますが、この「*」はなぜつけるのでしょうか?



struct Goods{
    char name[30];
    int price;
};

struct Goods *foo(struct Goods gs[/url]){
    int max = 0;
    int idx = 0;
    int i;
    for(i=0;i<5;i++){
        if(max < gs.price){
            max = gs.price;
            idx = i;
        }
    }
    return &gs[idx];
}

int main(void){
    struct Goods *pg;
    struct Goods gs[5] = {
        {"鉛筆",30},
        {"消しゴム",50},
        {"ノート",100},
        {"ボールペン",80},
        {"クリップ",20}
    };
    pg = foo(gs);
    printf("もっとも値段の高い商品は\n");
    printf("%s(%d円)\n",pg->name,pg->price);
    printf("です\n");

    return 0;
}

Re:構造体について

Posted: 2010年1月11日(月) 18:43
by box
> struct Goods *foo(struct Goods gs[/url]){

foo関数の戻り値の型が、
struct Goods型「へのポインタ」であることを示します。

何も構造体に限った話ではありません。

int *bar(~)

というのは、bar関数の戻り値の型が
int型「へのポインタ」であることを示します。

Re:構造体について

Posted: 2010年1月11日(月) 18:47
by くりく
ありがとうございます。
理解できました。