如何解决声明指向NULL的指针
当我声明:
struct str_node{
int data;
struct str_node *next;
}node;
node *head = NULL;
是这样的吗?
head = NULL;
或
*head = NULL
解决方法
每个声明都采用以下格式:
Type varName = initializer;
以您的情况
node * head = NULL;
^ ^ ^
| Type | | varName | = | initializer |;
因此,head
是类型为node *
(指向node
的指针)的变量。变量为head
,其值初始化为NULL
。
我想扩大罗伯托的答案。
此
node *head = NULL;
将不会编译,因为node
不是类型,它实际上是名为struct str_node
的变量node
。
如果要定义类型,请使用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
定义指针然后,将其值设置为NULL
:
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
,
欢迎您!
就像是
head = NULL;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。