I am just started learning linked list in c. I still confuse the line 1 in the code. 1. what is temp->data, is it pointer ? variable? 2. what is temp->next=head, here head has NULL value???? if so, temp->next become NULL now ??? really screwed on this lines please,help me. Thanks
我刚开始学习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;
}
3 个解决方案
#1
3
what is temp->data, is it pointer? variable?
什么是temp-> data,它是指针吗?变量?
Well, let's break it down:
好吧,让我们分解一下:
-
what is temp? It's declared as
什么是温度?它被宣布为
struct test_struct* temp = ...
so we know it is a pointer to
struct test_struct
.所以我们知道它是一个指向struct test_struct的指针。
-
what is
temp->data
? It means follow (dereference) the pointer, and get the member called data. Your test_struct is declared as什么是临时数据?它意味着跟随(取消引用)指针,并获得名为data的成员。您的test_struct声明为
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.所以,我们知道它有一个名为data的整数成员。 temp-> data是对该整数的引用。
what is temp->next=head, here head has NULL value???? if so, temp->next become NULL now ???
什么是temp-> next = head,这里head有NULL值????如果是这样,temp-> next现在变为NULL ???
This code assigns NULL to the pointer temp->next
.
此代码将NULL指定给指针temp-> next。
If you're confused about this stuff, learning to step through it in a debugger might help (as would a good book).
如果你对这些东西感到困惑,那么学习在调试器中逐步完成它可能会有所帮助(就像一本好书一样)。
#2
1
The ->
operator follows a pointer to a structure to reference one of the structure's elements. In this case:
- >运算符跟随指向结构的指针以引用结构的一个元素。在这种情况下:
temp->data = 5;
temp->next = head;
temp
is a pointer to a struct of type struct test_struct
, and that struct has members named data
and next
. These two statements assign values to those two members, in the struct pointed to by temp
.
temp是指向struct test_struct类型结构的指针,该结构具有名为data和next的成员。这两个语句在temp指向的结构中为这两个成员赋值。
Since head
was set to be NULL
earlier in the code, it is indeed the case that the second statement sets the next
member to be NULL
.
由于head在代码中较早被设置为NULL,因此第二个语句确实将下一个成员设置为NULL。
Technically speaking, each of temp->data
and temp->next
is an lvalue (pronounced "ell value"), which means that they can be used both for the value they reference as well as for the location of where the value is stored. The "l" stands for "left" as a mnemonic that suggests that these things are what you have on the left-hand side of an assignment statement.
从技术上讲,temp-> data和temp-> next中的每一个都是左值(发音为“ell value”),这意味着它们既可以用于它们引用的值,也可以用于存储值的位置。 “l”代表“左”作为助记符,表示这些东西就是你在赋值语句左侧的东西。
#3
1
What is your code does is it creates a variable name "temp" which is a type of test_struct*. then it allocate memory and point the variable temp to that memory piece. This temp variable is a pointer that points to memory piece you created using malloc. inside "temp" it have two variable name data and next. in C, to access a member you use -> operator. (Line 1) you saves integer 5 to data variable in temp. in Line 2, you assign NULL to next (at this point your head is null[get it! your head is null :)]). then you points the head to the memory piece where temp was pointing. now if you printf("%d", head->data) it will print 5.
你的代码是做什么的,它创建了一个变量名“temp”,它是一种test_struct *。然后它分配内存并将变量temp指向该内存块。此临时变量是指向使用malloc创建的内存块的指针。在“temp”里面它有两个变量名数据和下一个。在C中,访问您使用的成员 - >运算符。 (第1行)您将整数5保存到temp中的数据变量。在第2行中,你为下一个分配NULL(此时你的头是空的[得到它!你的头是空的:)])。然后你把头指向临时指向的记忆片。现在如果你printf(“%d”,head-> data)它会打印5。
I commented each line in your code. Hope this helps.
我评论了代码中的每一行。希望这可以帮助。
#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;
}
#1
3
what is temp->data, is it pointer? variable?
什么是temp-> data,它是指针吗?变量?
Well, let's break it down:
好吧,让我们分解一下:
-
what is temp? It's declared as
什么是温度?它被宣布为
struct test_struct* temp = ...
so we know it is a pointer to
struct test_struct
.所以我们知道它是一个指向struct test_struct的指针。
-
what is
temp->data
? It means follow (dereference) the pointer, and get the member called data. Your test_struct is declared as什么是临时数据?它意味着跟随(取消引用)指针,并获得名为data的成员。您的test_struct声明为
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.所以,我们知道它有一个名为data的整数成员。 temp-> data是对该整数的引用。
what is temp->next=head, here head has NULL value???? if so, temp->next become NULL now ???
什么是temp-> next = head,这里head有NULL值????如果是这样,temp-> next现在变为NULL ???
This code assigns NULL to the pointer temp->next
.
此代码将NULL指定给指针temp-> next。
If you're confused about this stuff, learning to step through it in a debugger might help (as would a good book).
如果你对这些东西感到困惑,那么学习在调试器中逐步完成它可能会有所帮助(就像一本好书一样)。
#2
1
The ->
operator follows a pointer to a structure to reference one of the structure's elements. In this case:
- >运算符跟随指向结构的指针以引用结构的一个元素。在这种情况下:
temp->data = 5;
temp->next = head;
temp
is a pointer to a struct of type struct test_struct
, and that struct has members named data
and next
. These two statements assign values to those two members, in the struct pointed to by temp
.
temp是指向struct test_struct类型结构的指针,该结构具有名为data和next的成员。这两个语句在temp指向的结构中为这两个成员赋值。
Since head
was set to be NULL
earlier in the code, it is indeed the case that the second statement sets the next
member to be NULL
.
由于head在代码中较早被设置为NULL,因此第二个语句确实将下一个成员设置为NULL。
Technically speaking, each of temp->data
and temp->next
is an lvalue (pronounced "ell value"), which means that they can be used both for the value they reference as well as for the location of where the value is stored. The "l" stands for "left" as a mnemonic that suggests that these things are what you have on the left-hand side of an assignment statement.
从技术上讲,temp-> data和temp-> next中的每一个都是左值(发音为“ell value”),这意味着它们既可以用于它们引用的值,也可以用于存储值的位置。 “l”代表“左”作为助记符,表示这些东西就是你在赋值语句左侧的东西。
#3
1
What is your code does is it creates a variable name "temp" which is a type of test_struct*. then it allocate memory and point the variable temp to that memory piece. This temp variable is a pointer that points to memory piece you created using malloc. inside "temp" it have two variable name data and next. in C, to access a member you use -> operator. (Line 1) you saves integer 5 to data variable in temp. in Line 2, you assign NULL to next (at this point your head is null[get it! your head is null :)]). then you points the head to the memory piece where temp was pointing. now if you printf("%d", head->data) it will print 5.
你的代码是做什么的,它创建了一个变量名“temp”,它是一种test_struct *。然后它分配内存并将变量temp指向该内存块。此临时变量是指向使用malloc创建的内存块的指针。在“temp”里面它有两个变量名数据和下一个。在C中,访问您使用的成员 - >运算符。 (第1行)您将整数5保存到temp中的数据变量。在第2行中,你为下一个分配NULL(此时你的头是空的[得到它!你的头是空的:)])。然后你把头指向临时指向的记忆片。现在如果你printf(“%d”,head-> data)它会打印5。
I commented each line in your code. Hope this helps.
我评论了代码中的每一行。希望这可以帮助。
#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;
}