二叉树实现在C语言中给了我分段错误
我在尝试访问存储在TreeNode中的数据时遇到分段错误。代码如下:二叉树实现在C语言中给了我分段错误,c,pointers,struct,segmentation-fault,binary-tree,C,Pointers,Struct,Segmentation Fault,Binary Tree,我在尝试访问存储在TreeNode中的数据时遇到分段错误。代码如下: #包括 #包括 类型定义结构节点tag{ int值; 结构NodeTag*LLink; 结构节点tag*RLink; }三烯醇; 无效顺序(树节点*n){ 如果(n->LLink!=NULL) 顺序(n->LLink); printf(“%d”,n->value); 如果(n->RLink!=NULL) 顺序(n->RLink); } void newNode(树节点*n,int v){ n=malloc(sizeof(Tre
#包括
#包括
类型定义结构节点tag{
int值;
结构NodeTag*LLink;
结构节点tag*RLink;
}三烯醇;
无效顺序(树节点*n){
如果(n->LLink!=NULL)
顺序(n->LLink);
printf(“%d”,n->value);
如果(n->RLink!=NULL)
顺序(n->RLink);
}
void newNode(树节点*n,int v){
n=malloc(sizeof(TreeNode));
n->值=v;
n->LLink=NULL;
n->RLink=NULL;
}
void addValue(TreeNode*r,int值){
如果(值值){
如果(r->LLink==NULL){
新节点(r->LLink,值);
}否则{
附加值(r->LLink,值);
}
}否则如果(值>r->值){
如果(r->RLink==NULL){
新建节点(r->RLink,值);
}否则{
addValue(r->RLink,value);
}
}
}
int main(){
树节点*root=0;
新节点(根,1);
printf(“%d\n”,root->value);//newNode
将返回一个指向已分配内存的指针,或者您可以向函数发送双指针并在其中分配内存
TreeNode* newNode(int v){
TreeNode *new_node = malloc(sizeof(TreeNode));
n->value = v;
n->LLink = NULL;
n->RLink = NULL;
return new_node
}
或
在C语言中,参数是通过值传递的。因此,调用newNode(r->LLink,value)
不会修改r->LLink
考虑一下这个简单的函数:
void Foo(int x)
{
x = x * 2 ;
}
调用Foo(n)
是否将n乘以2?否
您可能需要:
void Foo(int *x)
{
*x = *x * 2 ;
}
并调用Foo&n;
或:
并调用n=Foo(n);
在这段代码中,n
是指向TreeNode
结构的指针,但如果您将某个内容赋给n
,则在函数外部不可见,因为指针是按值传递的
void writeToA ( int a ) {
a = 5;
}
int main ( ) {
int x = 10;
writeToA(x)
printf("%d\n", x);
}
此代码将打印什么?它将打印10
,而不是5
。这是因为x
的值将传递给函数,而不是对x
的引用。在函数内更改该值不会在函数外更改x
的值
指针也是一个值,基本上是一个int,int值是一个内存地址:
void writeToPtr1 ( int * a ) {
int i = 10;
a = &i; // `a` now points to the memory address of i
}
void writeToPtr2 ( int * a ) {
*a = 5; // This doesn't change where `a` points to,
// it writes 5 to the memory address to that `a` points to.
}
int main ( ) {
int x = 10;
int *ptr = &x; // ptr now points to the memory address of x!
writeToPtr1(ptr);
// ptr still points to the memory address of x!
// As not a reference to ptr was passed, the memory
// address of x was passed to the function!
writeToPtr2(ptr);
// ptr still points to the memory address of x!
// But this memory now has the value 5 and not 10 anymore.
}
您需要返回分配结果:
TreeNode * newNode ( int v ) {
TreeNode * n = malloc(sizeof(TreeNode));
n->value = v;
n->LLink = NULL;
n->RLink = NULL;
return n;
}
int main ( ) {
TreeNode * root = newNode(1);
printf("%d\n", root->value);
return 0;
}
或者,您需要传递对指针的引用,然后更改指针指向的值:
void newNode ( TreeNode ** outNode, int v ) {
// TreeNode ** is a pointer to a pointer to a TreeNode!
TreeNode * n = malloc(sizeof(TreeNode));
n->value = v;
n->LLink = NULL;
n->RLink = NULL;
*outNode = n; // Make the pointer point to `n`
}
int main ( ) {
TreeNode * root = NULL;
newNode(&root, 1); // Pass a pointer to root
printf("%d\n", root->value);
return 0;
}
newNode()函数的作用与您认为的不同。请将其更改为返回新节点。
void writeToPtr1 ( int * a ) {
int i = 10;
a = &i; // `a` now points to the memory address of i
}
void writeToPtr2 ( int * a ) {
*a = 5; // This doesn't change where `a` points to,
// it writes 5 to the memory address to that `a` points to.
}
int main ( ) {
int x = 10;
int *ptr = &x; // ptr now points to the memory address of x!
writeToPtr1(ptr);
// ptr still points to the memory address of x!
// As not a reference to ptr was passed, the memory
// address of x was passed to the function!
writeToPtr2(ptr);
// ptr still points to the memory address of x!
// But this memory now has the value 5 and not 10 anymore.
}
TreeNode * newNode ( int v ) {
TreeNode * n = malloc(sizeof(TreeNode));
n->value = v;
n->LLink = NULL;
n->RLink = NULL;
return n;
}
int main ( ) {
TreeNode * root = newNode(1);
printf("%d\n", root->value);
return 0;
}
void newNode ( TreeNode ** outNode, int v ) {
// TreeNode ** is a pointer to a pointer to a TreeNode!
TreeNode * n = malloc(sizeof(TreeNode));
n->value = v;
n->LLink = NULL;
n->RLink = NULL;
*outNode = n; // Make the pointer point to `n`
}
int main ( ) {
TreeNode * root = NULL;
newNode(&root, 1); // Pass a pointer to root
printf("%d\n", root->value);
return 0;
}