Pointer problem sorting

Pointers seem to be a headache all the time, and the use of pointers from the first level to the second level is always wrong. I encountered a second-level pointer problem today. I spent a lot of time and thought of a more reasonable explanation.

In the experiment of data structure today, the establishment of a binary tree is written with a secondary pointer as a parameter. The approximate function code is as follows:

The storage structure is as follows:


/*树的存储结构*/
typedef struct Tree 
{
    char data;
    struct Tree *Lift;
    struct Tree *Right;
}Tree,*Btree;
/*二级指针建立二叉树*/
void crea(Btree *root)
{
    printf("%p\n",root);
    char ch;
    ch = getchar();
    if(ch=='#')
    {
        (*root) =  NULL;
    }
    else
    {
        *root=(Tree *)malloc(sizeof(Tree));
        (*root)->data = ch;
        // printf("ch=%ckk\n",ch);
        if((*root)==NULL)
        {
            exit(0);
        }
        crea((&(*root)->Right));
        crea((&(*root)->Lift));
    }
}

Then in order to test whether the function for creating a binary tree is correct, I simply defined a secondary pointer in the main function and passed it in, and then the problem came.

int main()
{
    Btree *p;
    crea(p);
}

Then the program, when it runs to malloc, breaks the error.

 *root=(Tree *)malloc(sizeof(Tree));
 printf("%c\n",ch);//该输出是输出不了的,以段错误结束

Then change the way the main function passes parameters, and you can malloc normally.

int main()
{
    Btree p; //一级指针
    crea(&p); //传入一级指针本身的地址
}

It is also equivalent to the second-level pointer, but why does the first type have a broken error. The main reason is to give a clear address after malloc a clear space.


The simplest example (first understand how the custom function parameters are passed)

#include<stdio.h>
#include<stdlib.h>

int fun(int a)
{
    a++;
    return a;
}

int main()
{
    int b = 0;
    int a = 10;
    fun(a);
    b = fun(b);
    printf("b=%d\n",b);
    printf("a=%d\n",a);
    return 0;
}

In the C language, the parameter transfer of the function is value transfer, the prime is fun(a), but the value of a is passed to a in the custom function, and then a is added in the custom function, but in the main function a is unchanged, but there is more return, and return will assign the result of the self-addition of a to b in the main function. The result is as follows:
write picture description here

But what is passed to this custom function is the address of a variable in a main function, then this a will be changed in the custom function.

#include<stdio.h>
#include<stdlib.h>

int fun(int *a)
{
    (*a)++;
    return *a;
}

int main()
{
    int b = 0;
    int a = 10;
    fun(&a);
    b = fun(&b);
    printf("b=%d\n",b);
    printf("a=%d\n",a);
    return 0;
}

write picture description here
In the second code, a pointer to an int type is defined in the formal parameter, and the address of a variable is passed in from the main function, then the formal parameter will get the address of a in the main function, and then directly in the a's address Manipulate its content on the address. In the first code, the formal parameter only obtains the value of a, but does not obtain its own address, so there is no right to change the variable a in the main function. In the second code, the value passed is the address value of a, and a is changed directly from the address value.

Then understand the pointer in (has it's own address value, and storage area)

address is 100 address is 200
stored content stored content

So my understanding of the secondary pointer (**p) is probably:

100 104 108
Store address 104 Store address 108 save a

So the first-level pointer is (*p):

100 104
Store address 104 a

Finally back to the original error code

int main()
{
    Btree *p;
    crea(p);
}

where p is a secondary pointer because Btree is *Btree. At this time, the second-level pointer is passed in, which is a pointer variable, and does not point to a certain space explicitly. After the custom function creat receives it, root points to p, and malloc opens up a clear space, then root will point to the first address of this random space, but before that, root points to p, and p does not point to A definite space, so malloc at this point will error out.

int main()
{
    Btree p;
    crea(&p);
}

In the second code, p is a first-level pointer variable, but when &p is passed in, if the address of the p pointer itself is 100, then 100 is passed to the variable root, which is a second-level pointer, yes The pointer variable that stores the pointer, at this time, 100 is stored in the root storage content. After malloc a piece of space, the pointer of this address stored in the root storage is lowered to point to the space opened up by malloc. At this point malloc assigns an explicit pointer. So can't go wrong.

Another simple analogy example

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *p;
    *p = 9;
    printf("p=%d\n",*p);
}

There will be a break error, because *p can store an address with a content of 9, but it cannot be directly assigned to it by dropping 9, because P does not explicitly point to a space at this time. You can use malloc to point it to a specific space before dropping 9 into it. as follows:

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *p;
    p = (int *)malloc(4);
    *p = 9;
    printf("p=%d\n",*p);
}

or this:

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int a = 9;//申请一个地址,初始化为9.
    int *p;
    //p = (int *)malloc(4);
    p = &a;
    printf("p=%d\n",*p);
}

So today's code can still do such a correction.

int main()
{
    Btree *p;
    Tree *q=NULL; //申请了一个地址,代号为p,初始化为NULL
    p = &q; //p明确存储了q的地址,因此**p传入之后有了明确指向
    crea(p); //所以成功传入并没有发生错误
}


I feel that my understanding of pointers needs to be improved. If there is a mistake in this explanation this time, I hope you can help me point it out.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326362465&siteId=291194637