Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C指针地址很奇怪_C_Pointers_Data Structures - Fatal编程技术网

C指针地址很奇怪

C指针地址很奇怪,c,pointers,data-structures,C,Pointers,Data Structures,对不起,我的英语不好,我已经为Splaying tree编写了C代码,但是当我将“Find()”操作放在main函数中时,我无法初始化树,也就是说,没有行“Find()”,使用GDB,当断点在行“MakeEmpty”时,我发现my_tree的值是“0x0”,但添加行“Find”,值是“0x7fffffffbeb0”。我知道Find()函数有问题,但我不明白为什么后面的语句会影响前面的语句 #include<stdio.h> #include "SPTree.c" void main

对不起,我的英语不好,我已经为Splaying tree编写了C代码,但是当我将“Find()”操作放在main函数中时,我无法初始化树,也就是说,没有行“Find()”,使用GDB,当断点在行“MakeEmpty”时,我发现my_tree的值是“0x0”,但添加行“Find”,值是“0x7fffffffbeb0”。我知道Find()函数有问题,但我不明白为什么后面的语句会影响前面的语句

#include<stdio.h>
#include "SPTree.c"

void main()
{
    Position my_tree;

    my_tree = MakeEmpty( my_tree );

    my_tree = Insert( 5, my_tree );
    my_tree = Insert( 6, my_tree );

    Position P = Find( 5, &my_tree );
}
和树节点

struct SPNode
{
    ElementType Element;
    SPTree Left;
    SPTree Right;
};
这是插页

SPTree Insert( ElementType X, SPTree tree )
{
    if( tree == NULL )
    {
        //此时 tree 是一棵空树,分配空间并创建一棵树
        tree = malloc( sizeof( struct SPNode ) );
        if( tree == NULL )
            printf("Out of space!!!\n");
        else
        {
            tree->Element = X;
            tree->Left = tree->Right = NULL;
        }
    }
    else
    {
        //递归插入 X 到合适的子树中
        if( X < tree->Element )
            tree->Left = Insert( X, tree->Left );
        else if( X > tree->Element )
            tree->Right = Insert( X, tree->Right );
        else
            //X已经在树中,什么也不做
            ;
    }
    return tree;
}
我在第1行调试断点,并打印我的_树的值为0x0,如下所示

请看这里:“请看Pic_1.png

但是在将Find()finction添加到main.c之后

#include<stdio.h>
#include "SPTree.c"

void main()
{
    Position my_tree;

    my_tree = MakeEmpty( my_tree );

    my_tree = Insert( 5, my_tree );
    my_tree = Insert( 6, my_tree );

    Position P = Find( 5, &my_tree );
}
#包括
#包括“SPTree.c”
void main()
{
定位我的树;
my_tree=MakeEmpty(my_tree);
my_tree=插入(5,my_tree);
my_tree=插入(6,my_tree);
位置P=查找(5和my_树);
}
我的_树的值如下:请参见上面链接中的Pic_2.png

为什么my_tree的值与前一行不同,只需将Find()添加到最后一行?

MakeEmpty()
函数返回指针或值

代码很奇怪。 我假设
MakeEmpty()
可以返回指向空
位置的指针

通常,您可以选择两种方式从函数返回指针:

1-检索指针作为传递参数

2-检索指针作为返回值

typedef foo_t;

foo_t foo;


pass_bar(foo_t **pass_foo)
{

    ...
    //Set the pointer to the **pass_foo parameter
} 


foo_t* return_foo(void)
{
   foo_t *my_foo;

   //Set the pointer my_foo to a valid memory address for foo_t 
   ...
   return my_foo;
}

您已经添加了标记
指针
,但在这段代码中,您不使用任何指针…

如果没有其他函数的代码,很难说0x7FFFFFFFBEB0:看起来它是在堆栈中分配的,但很难说。尝试
intx;printf(“%p”、&x)--它将在堆栈中为您提供一个地址为什么您需要
Find()
&
地址而不是
Insert()
?为什么您不希望
Find
操作修改树?它应该旋转树以将元素5带到根,这显然涉及到修改。这就是八字树的全部意义。@Weather-Vane:假设在旋转树的同时出现故障时能够返回NULL。插入没有这个问题,除非它们需要指出内存不足的情况。谢谢你的回复,很抱歉我的表达不好,我将查找和插入代码添加到这个问题中。编辑后。。。我不知道
MakeEmpty
,但是
Insert
,如果将空指针作为
tree
传递,则返回指向只有一个元素的
tree
结构的内存位置的指针。没有添加其他元素。。。然后,在接下来的两个函数调用中,只修改同一结构的
树->右
值。调用函数
Find()
,函数检查
tree->Left
是否为NULL,然后返回NULL
#include<stdio.h>
#include "SPTree.c"

void main()
{
    Position my_tree;

    my_tree = MakeEmpty( my_tree );

    my_tree = Insert( 5, my_tree );
    my_tree = Insert( 6, my_tree );

    Position P = Find( 5, &my_tree );
}
typedef foo_t;

foo_t foo;


pass_bar(foo_t **pass_foo)
{

    ...
    //Set the pointer to the **pass_foo parameter
} 


foo_t* return_foo(void)
{
   foo_t *my_foo;

   //Set the pointer my_foo to a valid memory address for foo_t 
   ...
   return my_foo;
}