Some common wrong usages of pointer in C language

Time:2021-4-20

A good pointer is like divine help. If you don’t use it well, you will complain. But most people don’t use it well, so many languages later encapsulated and shielded the pointer. For example, Java and Java do not have pointers, but pointers are used in many places, but they are not open to users. The mechanism of the language helps users handle the allocation and release of pointers, so as to facilitate users’ use and reduce errors. But we can’t stop eating because of choking. Although pointer is difficult, learning it well will greatly improve your programming ability.

Here is a list of common pointer errors in the learning process.

1. Pointer and pointer variable

  Many beginners do not understand the difference between the two, so many times they equate the two. In C language, pointer is a conceptual thing, simply speaking, address. Pointer variables are variable types, such as int and float, which are used to store pointers, that is, addresses. But most of the time, when we say pointer, we refer to pointer variable, which we need to know.

2. The address of the pointer variable and the address where the pointer variable is stored

  All variables need memory to store, and pointer variables are no exception. It also has an address, but the pointer variable itself stores the address. So many people will not know when it is the address of the pointer variable and when it is the address of the pointer variable, especially when there are secondary pointers or arrays.

 

3. Assign a value to the pointer without allocating space

  This is a very easy mistake for beginners. Let’s take a look at an example.

Char * p; / / define a pointer to store the string

Scanf (% s “, P); / / error will occur

At this time, the pointer P does not point to any effective space. It is clear that it is an effective space, not that it does not point to the memory space. When it is defined, P has a value. It is a random value and points to an unknown memory space, but it is illegal. When you access it, you will make an error. The correct way to write it is

char * p =( char *) malloc ( sizeof ( char )* size );// Size the amount of memory you need.

Scanf (% s “, P); / / note that it can’t be written as” & P “here, because P itself stores the address, so there’s no need to use the address symbol.

4. There is no space allocated when using secondary pointer to store string

 We often use string array to store a series of strings, but we can’t dynamically allocate the space size. We have to specify in advance, which is not good in many cases. So we need to use the pointer to store it. At this time, it’s easy to make mistakes.

Error 1:Char * * p; / / defines a secondary pointer to store a series of strings

    for(i=0;i

    {

P [i] = (char *) malloc (sizeof (char) * len)); / / number of len characters

    }

Error reason: P is a secondary pointer used to store a series of chars * Variable, but I didn’t allocate memory space for P and used it directly. I just give every P [ i ] Space is allocated, but p needs to be stored [ i ] The memory space of the address. That is, the memory space for each string address has not been allocated to P .

Error 2:

    Char * * p; / / defines a secondary pointer to store a series of strings

    p=(char**)malloc(sizeof(char*));

    for(i=0;i

    {

P [i] = (char *) malloc (sizeof (char) * len)); / / number of len characters

    }

Error reason: This is a common mistake in the way of many people on the Internet. Although P is allocated space here, only a char * type space is allocated. When the size exceeds 1, an error will occur.

Correct writing:

    char ** p ;// Define a secondary pointer to hold a series of strings

P = (char * *) malloc (sizeof (char * * * size)); / / allocate memory space of char * type

    for(i=0;i

    {

P [i] = (char *) malloc (sizeof (char) * len)); / / number of len characters

    }

5. Release space

 Free is used to free space in C language

Free (P); / / the space pointed to by P is released, but the memory space for P is not released.

P = null; / / this sentence is recommended

Space allocation:

Char * * p; / / defines a secondary pointer to store a series of strings

p =( char **) malloc ( sizeof ( char *)* size ));// Allocate a char * Type of memory space

    for(i=0;i

    {

P [i] = (char *) malloc (sizeof (char) * len)); / / number of len characters

    }

 Free space

    for(i=0;i

      free(p[i]);

    free(p);

    p=NULL;

Recommended Today

Large scale distributed storage system: Principle Analysis and architecture practice.pdf

Focus on “Java back end technology stack” Reply to “interview” for full interview information Distributed storage system, which stores data in multiple independent devices. Traditional network storage system uses centralized storage server to store all data. Storage server becomes the bottleneck of system performance and the focus of reliability and security, which can not meet […]