C 将指针声明为NULL

C 将指针声明为NULL,c,pointers,struct,C,Pointers,Struct,当我宣布: struct str_node{ int data; struct str_node *next; }node; node *head = NULL; 是不是有点像: head = NULL; 或 欢迎来到SO! 就像 head=NULL欢迎来到SO! 就像 head=NULL每个声明的格式如下: Type varName = initializer; 就你而言 node * head = NULL; ^ ^

当我宣布:

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

node *head = NULL;
是不是有点像:

head = NULL;

欢迎来到SO! 就像
head=NULL

欢迎来到SO! 就像
head=NULL

每个声明的格式如下:

Type varName = initializer;
就你而言

node *     head          =   NULL;
 ^          ^                 ^
| Type |  | varName | = | initializer |;

因此,
head
是一个变量,其类型为
node*
(指针指向
node
)。变量为
head
,其值初始化为
NULL

每个声明的格式如下:

Type varName = initializer;
就你而言

node *     head          =   NULL;
 ^          ^                 ^
| Type |  | varName | = | initializer |;

因此,
head
是一个变量,其类型为
node*
(指针指向
node
)。变量是
head
,它的值被初始化为
NULL

我想扩展Roberto的答案

这个

节点*head=NULL

不会编译,因为
节点
不是一个类型,它实际上是一个名为
节点
的变量。 如果要定义类型,请使用
typedef
说明符,如下所示

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

node *head = NULL;

我想扩大罗伯托的答案

这个

节点*head=NULL

不会编译,因为
节点
不是一个类型,它实际上是一个名为
节点
的变量。 如果要定义类型,请使用
typedef
说明符,如下所示

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

node *head = NULL;

定义指针并将其值设置为
NULL

int *head = NULL;
// conceptual    memory address    value
// head          0xABCD0000        0          
// that is;
// head is pointing to nowhere
int *head;
head = NULL;
// conceptual    memory address    value
// head          0xABCD0000        0          
// that is;
// head is pointing to nowhere
定义一个指针,然后将其值设置为
NULL

int *head = NULL;
// conceptual    memory address    value
// head          0xABCD0000        0          
// that is;
// head is pointing to nowhere
int *head;
head = NULL;
// conceptual    memory address    value
// head          0xABCD0000        0          
// that is;
// head is pointing to nowhere
定义一个指针,然后设置它指向
0
的内存地址:

int *head;
head = 0xDCBA0000; // or a "malloc" function 
*head = 0;
// conceptual    memory address    value
// head          0xABCD0000        0xDCBA0000          
// ...
// *head         0xDCBA0000        0
// that is;
// head is pointing to 0xDCBA0000
// and the value of the memory it's pointing at is set to zero

定义指针并将其值设置为
NULL

int *head = NULL;
// conceptual    memory address    value
// head          0xABCD0000        0          
// that is;
// head is pointing to nowhere
int *head;
head = NULL;
// conceptual    memory address    value
// head          0xABCD0000        0          
// that is;
// head is pointing to nowhere
定义一个指针,然后将其值设置为
NULL

int *head = NULL;
// conceptual    memory address    value
// head          0xABCD0000        0          
// that is;
// head is pointing to nowhere
int *head;
head = NULL;
// conceptual    memory address    value
// head          0xABCD0000        0          
// that is;
// head is pointing to nowhere
定义一个指针,然后设置它指向
0
的内存地址:

int *head;
head = 0xDCBA0000; // or a "malloc" function 
*head = 0;
// conceptual    memory address    value
// head          0xABCD0000        0xDCBA0000          
// ...
// *head         0xDCBA0000        0
// that is;
// head is pointing to 0xDCBA0000
// and the value of the memory it's pointing at is set to zero

是的,对不起,我没有正确地复制代码,但是我像你一样编写了代码。是的,对不起,我没有正确地复制代码,但是我像你一样编写了代码。