About C language uninitialized pointer and null pointer

The pointer p is defined and initialized at the same time, so that it can run normally, the code is as follows:

#include <stdio.h>
int main()
{
    
    
    int a;
    int *p=&a;
    scanf("%d",p);
    printf("%d",*p);
    return 0;
}

The pointer is initialized to null, and it cannot run normally at this time. The code is as follows:

#include <stdio.h>
int main()
{
    
    
    int *p=NULL;
    scanf("%d",p);
    printf("%d",*p);
    return 0;
}

It seems that null pointers cannot be used either.

New in 20200505
Some thoughts on pointers and dynamic memory allocation
1. The first address of the storage space occupied by the variable in the memory is called the address of the variable, and the data stored in the storage space of the variable becomes the value of the variable. (Before I didn’t read this sentence carefully. The addresses stored in the pointer variable p are all empty, how can I assign a value to *p? The skin does not have the feeling of attachment)
2. The function malloc() : Used to allocate several bytes of memory space and return a pointer to the first address of the memory. If the system cannot provide enough memory space, the function will return a null pointer NULL
3. Continue to use the memory after releasing the memory If the memory
is released but still continue to use it-will result in a wild pointer.
When the stack memory space pointed to by the pointer is released, the pointer pointing to it does not die. After the memory is released, the value of the pointer does not actually change. It still points to this memory, but the data stored in the memory becomes random Value (garbled) only. The result of releasing the memory only changes the data stored in the memory, making the content stored in the memory a hot chicken, and the pointer to the garbage memory is called a wild pointer.
After the memory is released, the pointer to it will not automatically become a null pointer, and the wild pointer is not a null pointer

Guess you like

Origin blog.csdn.net/weixin_43919570/article/details/105555298