C Advanced Development Notes - Development Notes 28: Pointer and Array Analysis (Part 1)

--The difficulty of things is far less than the fear of things!

   In this chapter, we will analyze pointers and arrays. In the first two chapters, we talked about arrays and pointers. We know:

    The nature of the array:

        - An array is a contiguous memory space

        - The space size of the array is sizeof( array_type ) * array_size

        - The array name can be thought of as a constant pointer to the first element of the array

    So let's look at a question: for an array int array[], what is array + 1? What does this operation mean?

    Below is the code to show

    

#include <stdio.h>

intmain()
{
    int array[5] = {1,2,3,4,5};

    int i=0;
	
    for(i=0; i<5; i++)
    {
	printf("0x%p -> %d\n", &array[i], array[i]);
    }
    printf("**************\n");

    printf("array + 1 = 0x%p\n*(array + 1) = %d\n", array + 1, *(array + 1));
    
    return 0;
}

The results of compilation and execution are as follows:


From the output, we can know that array + 1 == &array[1] , *( array + 1) == array[1].

In order to understand the above two equations, let's first understand the operation of pointers:

    - A pointer in the C language is a special variable, and its operation rules with integers are: 

        p+n  -> (unsigned int)p + n*sizeo(*p)

    - When the pointer p points to an array element of the same type: p + 1 will point to the next element of the current element; p - 1 will point to the previous element of the current element

    - The same can be extended: p + i will point to the i-th element after the current element; p - will point to the i-th element before the current element

So for array + 1 in the above code, array is the address of the first element of the array, then array + 1 points to the next element of the first element of the array, which is array[1], so there is array + 1 == &array[ 1] , *( array + 1) == array[1], the same can be deduced: array + i == &array[i] , *( array + i) == array[i] (i is the array subscript)

    Above we only talked about the operation between pointers and integers, so can operations be performed between pointers and pointers? in C language

- Only subtraction is supported     between pointers

- The pointer types     involved in the subtraction must be the same

    p1 - p2 -> ( (unsigned int)p1 - (unsigned int)p2 ) / sizeof(type)

    Among them, it should be noted that:

        - Only when two pointers point to elements in the same array, pointer subtraction makes sense, which means the subscript difference of the elements pointed to by the pointers

        - When the elements pointed to by the two pointers are not in the same array, the result is undefined

    Comparing pointers:

        - Pointers can also perform relational operations ( <, <=, >, >=)

        - The premise of pointer relational operation is to point to elements in the same array at the same time

- Unlimited comparison operations (==, !=)         between any two pointers , and such comparison operations have no practical significance

        - The pointer types participating in the comparison operation must be the same

   Summarize:

        - The compiler automatically allocates a contiguous memory space when the array is declared

- The pointer is declared with only 4 bytes of space         allocated to hold the address value

        - Pointers and integers can be operated on, and the result of the operation is a pointer

        - Only the subtraction operation is supported between pointers, and the result is the subscript difference of the array elements

        - Comparison operations are supported between pointers, and the pointer types must be the same

Guess you like

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