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

what is temp->data, is it pointer? variable?

Well, let's break it down:

  1. what is temp? It's declared as

    struct test_struct* temp = ...
    

    so we know it is a pointer to struct test_struct.

  2. what is temp->data? It means follow (dereference) the pointer, and get the member called data. Your test_struct is declared as

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

    so, we know it has an integer member called data. temp->data is a reference to that integer.


what is temp->next=head, here head has NULL value???? if so, temp->next become NULL now ???

This code assigns NULL to the pointer temp->next.

If you're confused about this stuff, learning to step through it in a debugger might help (as would a good book).

于 2013-11-12T18:37:33.457 回答
1

您的代码所做的是它创建了一个变量名称“temp”,它是一种 test_struct*。然后它分配内存并将变量 temp 指向该内存块。这个临时变量是一个指针,指向您使用 malloc 创建的内存块。在“temp”里面它有两个变量名数据和下一个。在 C 中,访问您使用 -> 运算符的成员。(第 1 行)您将整数 5 保存到 temp 中的数据变量中。在第 2 行中,您将 NULL 分配给 next(此时您的 head 为 null [明白了!您的 head 为 null :)])。然后你将头部指向 temp 指向的内存片段。现在如果你 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

运算符跟随指向结构的->指针以引用结构的元素之一。在这种情况下:

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

temp是指向 struct 类型的指针struct test_struct,并且该 struct 具有名为dataand的成员next。这两个语句在 指向的结构中为这两个成员赋值temp

由于在代码head中设置为NULL较早,因此第二条语句确实将next成员设置为NULL

从技术上讲,每个temp->datatemp->next都是一个左值(发音为“ell value”),这意味着它们既可以用于它们引用的值,也可以用于存储值的位置。“l”代表“left”作为助记符,表明这些东西是你在赋值语句左侧的东西。

于 2013-11-12T18:32:20.413 回答