構造体について

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
くりく

構造体について

#1

投稿記事 by くりく » 15年前

いま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;
}

box

Re:構造体について

#2

投稿記事 by box » 15年前

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

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

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

int *bar(~)

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

くりく

Re:構造体について

#3

投稿記事 by くりく » 15年前

ありがとうございます。
理解できました。

閉鎖

“C言語何でも質問掲示板” へ戻る