初始化时是否有必要调用指针= NULL? [英] is it necessary to call pointer = NULL when initializing?

查看:135
本文介绍了初始化时是否有必要调用指针= NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建指向某个结构的指针时,是否必须将其设置为NULL,然后分配它然后使用它?为什么?

when I create a pointer to certain struct, do I have to set it to NULL, then alloc it then use it? and why?

推荐答案

否,声明语言时,没有要求(就语言而言)初始化任何指针变量.因此

No, there is no requirement (as far as the language is concerned) to initialize a pointer variable to anything when declaring it. Thus

T* ptr;

是有效的声明,它引入了具有不确定值的名为ptr的变量.您甚至可以以某种方式使用该变量,而无需先分配任何内容或将其设置为任何特定值:

is a valid declaration that introduces a variable named ptr with an indeterminate value. You can even use the variable in certain ways without first allocating anything or setting it to any specific value:

func(&ptr);

这篇关于初始化时是否有必要调用指针= NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆