#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//構造体の定義
struct testdat {
char id[10];
char name[64];
int test[6];
struct testdat *p;
};
//構造体のデータを追加する
struct testdat *addlist(char *id, char *name, int t1, int t2, int t3, int t4, int t5, struct testdat *head);
void showlist(struct testdat *p); //登録されたデータイストの表示
void freelist(struct testdat *p); //プログラム終了時に使用したメモリ開放
int main(void) {
struct testdat *head; //先頭ポイントの設定
char idin[10], namein[64]; int testin[5];
head = NULL;
puts(" *** 自己参照struct(リストデータ)の作成 ***\n"); /* prints */
puts(" -----------データの入力\n");
printf(" 半角スペース区切りで、\nID 名前 国語 数学 英語 理科 社会 の点数を入力\n(終了=Ctrl+Z\n");
fflush(0);
while ( scanf("%s %s %d %d %d %d %d", idin, namein, &testin[0], &testin[1], &testin[2], &testin[3], &testin[4])!=EOF) {
//リストにデータを登録
head = addlist(idin, namein, testin[0], testin[1], testin[2], testin[3], testin[4], head);
}
puts("****** 入力されたデータ");
showlist(head);
puts("------------メモリの開放");
freelist(head);
return EXIT_SUCCESS;
}
/* データをリストに追加する */
struct testdat *addlist(char *id, char *name, int t1, int t2, int t3, int t4, int t5, struct testdat *head){
struct testdat *p;
//記憶領域の確保
if ((p = (struct testdat *) malloc(sizeof(struct testdat)))==NULL) {
printf("メモリ確保できません\n");
exit(EXIT_FAILURE);
}
strcpy(p->id, id); strcpy(p->name, name);
p->test[0]=t1; p->test[1]=t2; p->test[2]=t3; p->test[3]=t4; p->test[4]=t5;
p->test[5]=t1+t2+t3+t4+t5;
//自己参照ポインタの付け替え
p->p=head;
head = p;
return head;
}
/*記憶されたデータの表示 */
void showlist(struct testdat *p) {
while(p != NULL) {
printf("%s %s %d %d %d %d %d %d\n", p->id, p->name, p->test[0], p->test[1], p->test[2], p->test[3], p->test[4], p->test[5]);
}
return;
}
/* 使用しための開放 */
void freelist(struct testdat *p) {
struct testdat *p2;
while(p!=NULL) {
p2 = p->p;
free(p);
p = p2;
}
return;
}
ですが、ポインタがどんなふうに変化しているのか詳細に教えてくださいませんか。
A 回答 (2件)
- 最新から表示
- 回答順に表示
No.2
- 回答日時:
フツーに、ポインタ(の内容?)が変化する個所でprintfでアドレス表示すれば良いのでは。
例えば、main関数だと、
//リストにデータを登録
printf("before addlist, head = %p\n", head);
head = addlist(idin, namein, testin[0], testin[1], testin[2], testin[3], testin[4], head);
printf("after addlist, head = %p\n", head);
だとか。
No.1
- 回答日時:
まぁ、ハッキリ言うと、大方の人の反応は
「知らんがな」
だろうねぇ。
何を入力したいの?
何をやりたいの?
貴方「しか」知らん事を尋ねられても「知らんがな」ってのが答え。
テレパシストなんざいないんで、貴方が何を考えてるのか、脳内を覗く事は出来ない、からだ。
だから前回の質問、
https://oshiete.goo.ne.jp/qa/12696871.html
は誰もレスを付けなかっただろ?
「忖度する」のは回答者側の仕事じゃないんだよ。
いや、キチンとコードを書いてるのは偉いと思うよ。
ただし、入力を伴っていて「何かが入力される」前提だと、「知らんがな」になっちゃうんだよ。
分かる?
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- C言語・C++・C# c言語の問題です 3 2023/01/10 16:15
- C言語・C++・C# c言語の問題の説明、各所ごとに 5 2023/07/26 11:03
- C言語・C++・C# バイナリファイルをコピーするのにかかる時間を測りたいのですが実行するとFatel error:gli 2 2022/11/03 01:10
- C言語・C++・C# C言語 プログラミング 4 2022/05/22 11:53
- 大学・短大 C言語線形リストの問題です 3 2022/12/22 00:45
- C言語・C++・C# 未解決の外部シンボル _printfが関数_mainで参照されました 1 2022/09/18 15:28
- C言語・C++・C# カードシャッフルのブログラムを使ってc言語でブラックジャックをしたい 2 2022/04/12 15:13
- C言語・C++・C# C言語初心者 構造体 課題について 1 2023/03/10 19:30
- C言語・C++・C# C言語初心者 構造体 課題について 2 2023/03/10 19:48
- C言語・C++・C# プログラミング c言語 4 2023/03/07 01:05
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
charでの計算?
-
charからLPTSTRへの変換方法
-
文字列から空白を取り除きたい...
-
double型の値をchar配列に変換...
-
c言語でユーザ関数を利用して入...
-
テキストデータをそのままバイ...
-
C言語について。
-
CStringをwchar_tに変換したい
-
enumの記述について。
-
アルファベットの並べ替え
-
'const char *' 型は 'char *' ...
-
3桁区切(コンマ)記号をつけ...
-
C言語のポインターで詰まっている
-
switch文で文字を比較すること...
-
pingの結果を取得する方法
-
配列をnビットシフトする
-
for文 アスタリスクで形を作る
-
ncursesで...
-
C言語の入力した文字を反転させ...
-
Visual Studio strcpyについて
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
fgetsなどのときのstdinのバッ...
-
C言語のfor文です。 繰り返しの...
-
テキストデータをそのままバイ...
-
charでの計算?
-
文字列から空白を取り除きたい...
-
charからLPTSTRへの変換方法
-
atoi( ) の反対をやりたい
-
2曲同時再生するにはどうした...
-
C言語の入力した文字を反転させ...
-
配列をnビットシフトする
-
3桁区切(コンマ)記号をつけ...
-
int main()の・・・
-
型変換
-
CStringをwchar_tに変換したい
-
'const char *' 型は 'char *' ...
-
間接操作のレベルとは
-
double型の値をchar配列に変換...
-
絶対パスからのファイル名の切...
-
switch文で文字を比較すること...
-
c++ 文字列を入力して、一文字...
おすすめ情報