C 有人能解释一下吗对于像我这样学习缓慢的人?
我已经花了好几个小时寻找一个好的解释,但不知何故,我似乎不明白。我很难理解C 有人能解释一下吗对于像我这样学习缓慢的人?,c,pointers,struct,C,Pointers,Struct,我已经花了好几个小时寻找一个好的解释,但不知何故,我似乎不明白。我很难理解->指针的概念。目前,我正在尝试理解以下代码,这些代码在单个链接列表中创建一个新节点: // structure to create every node in the list struct LinkedList{ int data; // // is part of a node and points to the next node struct LinkedList *next; //Poi
->
指针的概念。目前,我正在尝试理解以下代码,这些代码在单个链接列表中创建一个新节点:
// structure to create every node in the list
struct LinkedList{
int data; //
// is part of a node and points to the next node
struct LinkedList *next; //Pointer to address of next node
};
typedef struct LinkedList *node;
node createnode(){
node temp; //declare a node
//allocate memory with size of the struct
temp = (node) malloc(sizeof(struct LinkedList));
temp->next = NULL; //next points to NULL
return temp; //return new node
}
temp->next=NULL
具体做什么?我知道它与(*temp).next=NULL
相同,但它也与:
next = NULL;
*temp = next;
编辑:更正标题
temp->next
与(*temp.)相同。next
。
调用next=NULL
也是错误的,正确的形式是(*temp)。next=NULL
或
temp->next=NULL
我来试试这个
您已经知道temp->next=NULL
与(*temp)相同。next=NULL
。这是理解会员访问操作员的一个很好的指南->
首先是:
typedef struct A_{
int x;
}A;
A obj; //simple vanilla struct instance
A* ptr; //simple pointer to struct
让我们来讨论这一部分:
ptr->x = 314
ptr->x
行中的任何代码都可以通过两个步骤来考虑
第一:
A temp_ = *ptr;
取消引用ptr
以获取类型为struct A
其次是:
temp_.x = 314;
将314的值分配给结构的成员x
,temp\u
,就像有一个简单的struct
实例,如obj,x=314
一样
考虑到这一点,让我们来解决您的备选方案:
next = NULL;
这没有多大意义,因为next
本身没有任何地位。它是结构链接列表的一部分。所以这是不可能的
*temp = next
这同样没有帮助,因为*temp
取消引用指针以获取struct LinkedList
的实例,并将next
赋值给它是不正确的语法,因为next
是指向struct LinkedList
的指针,而不是struct LinkedList
的实例
temp->(next=NULL)
按照上面相同的步骤解构
A temp_ = *temp;
temp_.(next = NULL)
正如可以观察到的,这在语法上是不正确的
希望这有帮助。temp
是一个在内存中有类似于0x1234
的邮政地址的房子。我们需要从房子里拿些东西,这样我们就把里面的东西忘了。要进入内部,我们应该通过预加星号的值返回主页,在本例中为*temp
。现在,我们可以访问房子里我们想要访问的任何东西,next
是房子里我们想要访问的对象,因为我们通过
操作符在里面,也就是说,(*temp).next
->
操作员模拟上述情况。所以下面的表达式是相等的
(*temp).next = NULL
temp->next = NULL
不,它和这些都不一样,因为那里没有名为next
的变量next
是struct LinkedList
中的成员,而不是它自己的对象。temp->next
的意思是“temp
是指向对象的内存地址(指针)。取消对指针的引用(->
)以获取对象,然后使用membernext
”temp->->
与(*temp>相同.下一步
(在第二件事中,括号必须在那里,因为)我喜欢这个标题。这回答了你的问题吗?次要的挑剔,你应该用struct A
或typedef
替换A
,struct
,因为这是一个C问题,而不是C++@mediocrevegetable1是的,在typedef
中添加了,并删除了所有提到的object。答案确实足以让这个问题重复。如果我是新手,我从中什么也得不到。