c - 指向结构的指针在声明时总是设置为 NULL 吗?

标签 c struct null

我正在声明一个指向结构的指针。当我打印它的值时,它没有设置为 NULL,我得到随机数。这是正常的吗?你只需要看第 3 行的 printf 就可以了。我什至没有使用这个指针。我宣布它只是为了看看会发生什么,因为我得到了错误的值(value)观。我没有任何其他同名指针,并且此函数被正确调用。

child * find_closer(int cid,child * root){
child *closer,*right,*left,*min,*not;
if(not!=NULL)
printf("    %d   ",not);
if(root!=NULL){
    if(root->cid < cid)
        closer=root;
    else
        min=root;
    if(root->lc!=NULL){
        if(closer==NULL || (root->lc->cid  <  cid  && closer->cid < root->lc->cid))
            closer=root->lc;
        if(min==NULL || min->cid > root->lc->cid)
            min=root->lc;
    }
    if(root->rc!=NULL){
        if(closer==NULL || (root->rc->cid  <  cid  && closer->cid < root->rc->cid))
            closer=root->rc;
        if(min==NULL || min->cid > root->rc->cid)
            min=root->rc;
    }
    if(closer==NULL){
        closer=min;
    }
    right=find_closer(cid,root->rc);
    left=find_closer(cid,root->lc); 
    if(closer->cid < cid){ 
        if(right!=NULL &&right->cid < cid && right->cid > closer->cid)
            closer=right;
        if(left!=NULL && left->cid < cid && left->cid > closer->cid)
            closer=left;
    }
    if(closer->cid<cid){
        return closer;
    }else{
        if(right!=NULL &&closer->cid > right->cid)
            closer=right;
        if( left!=NULL && closer->cid > left->cid)
            closer=left;
        return closer;
    }
}else
return NULL;

最佳答案

唯一保证在 C 中清零的声明是全局和静态声明。如果您在函数中声明一个变量,它将不会被初始化。

关于c - 指向结构的指针在声明时总是设置为 NULL 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34389065/

相关文章:

c - 如何为我的自定义函数生成非空参数警告?

c# - PInvoke 结构/函数中的奇怪错误

c - 从函数修改结构?

mysql - 带有 NULL 的 SQL CASE 语句

javascript - URL.createObjectURL 返回一个带有 null 前缀的 blob。例如 : Blob:null/12415-63

powershell - 在 Powershell 中重定向到 $null 奇怪的行为

c++ - 使用 LDAP 和 Windows Active Directory 进行用户身份验证 (Windows Server 2016)

c - 是否可以使用 libcurl 同时下载和发布 multipart/form-data 同一文件?

c - 尝试 'free' C 中的链表

struct - 如果在 Go 中构造,则无法初始化结构