ページ 11

構造体のポインタについて

Posted: 2012年4月03日(火) 20:46
by N_Angelo
構造体のポインタの書き方について質問です。

コード:

typedef struct{
int *no;
char *name;
}student;
といった書き方を見ますが、これは

コード:

typedef struct{
int no;
char name;
}student;

student data,*pdata;
pdata = &data;
と何が違うのでしょうか?
メンバ変数に*をつける意味がよくわからないので、ここで質問させていただきました。

Re: 構造体のポインタについて

Posted: 2012年4月03日(火) 20:52
by beatle
まったく違います。
前者の、メンバ変数をポインタにした場合は、配列へのポインタを格納したりできます。
対して、後者の場合はint型、char型の整数を入れることしかできません。

コード:

typedef struct {
    int *no;
} student;

student s;
int x[128];
s.no = x;

Re: 構造体のポインタについて

Posted: 2012年4月03日(火) 21:05
by box
構造体のメンバーではない場合の

コード:

    int no;
    int *no;
の区別は付いていますよね。
構造体のメンバーである場合も、全く同じことです。
構造体のことをむずかしく考えすぎていませんか?

Re: 構造体のポインタについて

Posted: 2012年4月03日(火) 21:26
by N_Angelo
>beatleさん
お返事ありがとうございます。
beatleさんの情報を元に色々調べてみようと思います。

>boxさん
お返事ありがとうございます。
boxさんの情報を元にいくと、

コード:

typedef struct{
int no;
char name;
}student;

student data;
data.no;
のdata.noと

コード:

int no;
char name;
のnoは同じで、

コード:

typedef struct{
int *no;
char *name;
}student;

student data;
data->no;
のdata->noと

コード:

int *no;
char *name;
のint *noは同じということでしょうか?
後、書き忘れていましたが、使用している言語はC言語です。

Re: 構造体のポインタについて

Posted: 2012年4月03日(火) 21:39
by box
N_Angelo さんが書きました:

コード:

typedef struct{
    int *no;
    char *name;
}student;

student data;
data->no;
変数dataはstudent型の実体である(student型へのポインターではない)ため、
data->no
という書き方はできません。
student型(構造体型)へのポインターの話と、構造体のメンバーがポインターである話とが
ゴッチャになっていないでしょうか。

Re: 構造体のポインタについて

Posted: 2012年4月03日(火) 21:59
by N_Angelo
言われて気づきましたが、ゴッチャになってますね…。
構造体型へのポインタとメンバ変数がポインタである場合の部分を勉強してきます…。
box さんが書きました:
N_Angelo さんが書きました:

コード:

typedef struct{
    int *no;
    char *name;
}student;

student data;
data->no;
変数dataはstudent型の実体である(student型へのポインターではない)ため、
data->no
という書き方はできません。
student型(構造体型)へのポインターの話と、構造体のメンバーがポインターである話とが
ゴッチャになっていないでしょうか。