0

cでリンクリストの学習を始めたばかりです。コードの1行目はまだ混乱しています。1. temp->data とは何ですか? ポインタですか? 変数?2. temp->next=head とは何ですか? ここで head には NULL 値があります???? もしそうなら、temp->next は今 NULL になる ??? この行に本当にめちゃくちゃになってください、助けてください。ありがとう

#include <stdio.h>
#include <stdlib.h>

struct test_struct{
    int data;
    struct test_struct *next;
};

struct test_struct* head=NULL;

int main()
{

    head = NULL;
    struct test_struct* temp = (struct test_struct*)malloc(sizeof(struct test_struct));
    if(NULL==temp)
    {
        printf("error in memory");
        return 0;
    }
    temp->data=5;    // line  1      <----------  what's going on
    temp->next=head; // line 2       <----------  what's going on here?
    head=temp;
    printf("%p\n",head);
    return 0;
}
4

3 に答える 3

3

temp->data とは何か、ポインタですか? 変数?

さて、それを分解しましょう:

  1. テンプとは?それは次のように宣言されています

    struct test_struct* temp = ...
    

    へのポインタであることがわかりますstruct test_struct

  2. は何temp->dataですか?ポインタをたどって(逆参照して)、 data というメンバを取得するという意味です。あなたの test_struct は次のように宣言されています

    struct test_struct {
      int data;
      struct test_struct *next;
    };
    

    したがって、データと呼ばれる整数メンバーがあることがわかります。temp->dataその整数への参照です。


temp->next=head とは何ですか? ここで head には NULL 値がありますか???? もしそうなら、temp->next は今 NULL になる ???

このコードはポインタに NULL を割り当てtemp->nextます。

このことについて混乱している場合は、デバッガーでステップスルーすることを学ぶことが役立つかもしれません (良い本と同様)。

于 2013-11-12T18:37:33.457 に答える
1

あなたのコードは、test_struct* の型である変数名「temp」を作成することです。次に、メモリを割り当て、変数 temp をそのメモリ ピースにポイントします。この一時変数は、malloc を使用して作成したメモリ ピースを指すポインタです。「temp」内には、2 つの変数名 data と next があります。C では、-> 演算子を使用してメンバーにアクセスします。(1 行目) 整数 5 を temp のデータ変数に保存します。2 行目で、next に NULL を割り当てます (この時点で、頭は null です [get it! your head is null :)])。次に、一時が指していたメモリピースに頭を向けます。printf("%d", head->data) を実行すると、5 が出力されます。

コードの各行にコメントを付けました。お役に立てれば。

#include <stdio.h>
#include <stdlib.h>
struct test_struct{
    int data;
    struct test_struct *next;
};
struct test_struct* head = NULL; //Creates a global variable head. its type is test_struct* and it is currently set to NULL
int main(){
    head = NULL; //
    struct test_struct* temp = (struct test_struct*)malloc(sizeof(struct test_struct));// creates a variable named temp which is a test_struct* type.
    if(NULL==temp){ //at this point if temp is NULL, it imply that above line failed to allocate memory so there is no point executing this program so we return.
        printf("error in memory");
        return 0;
    }
    temp->data=5; // in the structure test_struct there is a member variable data inside it and since the temp variable is of type test_struct, temp also has data member. so we can access it by using -> operator. since data is a integer type we can assign a number to it.
    temp->next=head; //At this point head is NULL. So we are pretty much assigning NULL to temp->next
    head=temp; //When using link list we usually keep the head pointing to the beginning of the link list.(unless its a circular link list). This line does the exact same. It points the dead to memory piece that temp pointing to. 
    printf("%p\n",head);
    return 0;
}
于 2013-11-12T19:05:35.143 に答える
1

演算子は、構造体への->ポインターに従って、構造体の要素の 1 つを参照します。この場合:

temp->data = 5;
temp->next = head;

tempは 型の構造体へのポインターstruct test_structであり、その構造体には および という名前のメンバーがdataありnextます。これら 2 つのステートメントは、 が指す構造体で、これら 2 つのメンバーに値を割り当てますtemp

headはコードの前の方でに設定されているため、NULL実際には 2 番目のステートメントでnextメンバーが に設定されますNULL

技術的に言えば、temp->datatemp->nextはそれぞれ左辺値(「エル値」と発音) です。つまり、参照する値と、値が格納される場所の両方に使用できます。"l" は "left" を表すニーモニックであり、代入文の左側にこれらのものが含まれていることを示します。

于 2013-11-12T18:32:20.413 に答える