dポイントプレゼントキャンペーン実施中!

#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件)

フツーに、ポインタ(の内容?)が変化する個所で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);

だとか。
    • good
    • 0

まぁ、ハッキリ言うと、大方の人の反応は



「知らんがな」

だろうねぇ。

何を入力したいの?
何をやりたいの?

貴方「しか」知らん事を尋ねられても「知らんがな」ってのが答え。
テレパシストなんざいないんで、貴方が何を考えてるのか、脳内を覗く事は出来ない、からだ。
だから前回の質問、

https://oshiete.goo.ne.jp/qa/12696871.html

は誰もレスを付けなかっただろ?
「忖度する」のは回答者側の仕事じゃないんだよ。

いや、キチンとコードを書いてるのは偉いと思うよ。
ただし、入力を伴っていて「何かが入力される」前提だと、「知らんがな」になっちゃうんだよ。
分かる?
    • good
    • 2

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!