• 请不要在回答技术问题时复制粘贴 AI 生成的内容
EchoChan
V2EX  ›  程序员

一个局部指针的问题:局部指针在不同的块不能共用?

  •  
  •   EchoChan · Jan 25, 2015 · 2755 views
    This topic created in 4215 days ago, the information mentioned may be changed or developed.

    //具有插入功能的查找(非递归)
    void BSTree::inst1(Sstudent el)
    {
    BTreeNode *p,*q;
    int tag;
    tag=0;
    p=root;
    while ((p != NULL)&& (tag == 0))
    {
    q= p;
    if (el < p->data)
    p=p->lchild;
    else if (el > p->data)
    p=p->rchild;
    else
    tag=1;
    }
    if (tag == 0)
    {

    p=new BTreeNode(el,NULL,NULL);
    if (root==NULL) root=p;
    else if (el < q->data) q->lchild= p;
    else q->rchild=p;
    }
    }

    刚才在VS2013编译的时候,提示 error C4703: potentially uninitialized local pointer variable 'q' used (22行)。
    q的作用范围不是整个函数吗?

    heliumhgy
        1
    heliumhgy  
       Jan 25, 2015 via Android
    错误是q可能会是一个未初始化的指针啊,如果while没执行,q就没有指向一个合法的内存空间啊,然后else分支里赋值就不行了
    EchoChan
        2
    EchoChan  
    OP
       Jan 26, 2015
    @heliumhgy 原来如此,谢谢啊,我没注意while没执行的情况。这句是从书上的模板改的。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3775 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 00:47 · PVG 08:47 · LAX 17:47 · JFK 20:47
    ♥ Do have faith in what you're doing.