c - 数据类型* <变量名> 和。数据类型 *<变量名>

标签 c pointers struct linked-list

<分区>

我正在研究如何在 C 中创建链表。看看 this article .

首先,他使用以下代码创建结构;

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

很明显*next是node类型的指针变量。

但是当他往前走的时候,他是这样做的;

struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;

现在我无法理解他要做什么;他是在创建名称、头、第二和第三个节点吗?或者他只是想创建节点类型的指针变量?

因为他把它们设为 NULL;我假设他正在尝试创建指针变量。但是他不能用这个做同样的事情吗?

struct node *head = NULL;
struct node *second = NULL;
struct node *third = NULL;

谢谢

最佳答案

在 C 语言中,* 前后的空格是没有意义的。所以:

struct node *head;
struct node * head;
struct node* head;
struct node*head;

完全一样。 C 不关心那个空格。

当您声明多个项目时,您就会遇到麻烦:

struct node *head, tail; // tail is not a pointer!
struct node *head, *tail; // both are pointers now
struct node * head, * tail; // both are still pointers; whitespace doesn't matter

关于c - 数据类型* <变量名> 和。数据类型 *<变量名>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25953669/

相关文章:

c - 如何用 C 处理 PostgreSQL 中的数字数据类型?

c - 是否可以在 C 中为 uint64_t 指针添加下标?

c - 结构声明 : Valid initializer in declaration?

c++ - struct c++ 中的默认参数

c - 下面的内存分配有什么不同吗?

c - void main() { if(sizeof(int) > -1) printf ("true");否则 printf ("false"); ;

c - 在 Windows 上显示带有自定义按钮标题的警报?

c - 在同一个指针上重复调用

c - 从函数返回指针(变量在函数中声明)

json - Golang/Go - 如果结构没有字段,如何将结构编码(marshal)为 null?