Pointer and Array Analysis

The nature of arrays:
(1) An array is a continuous piece of memory space
(2) The size of the array is sizeof(array_type)*array_size
(3) The array name can be viewed as a constant pointer to the first element of the array

Operation of Pointer
The rules for pointer and integer operations are:

p+n;	(unsigned int)p+n*sizeof(*p);

Subtraction between pointers
(1) Only subtraction between pointers is supported
(2) The pointer types involved in subtraction operations must be the same

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

Be careful:
1. Pointer subtraction makes sense only when two pointers point to elements in the same array, meaning the following table difference of the elements that the pointer points to
2. Structures are undefined when two pointers point to elements that are not in the same array
3. Pointers can also perform relational operations (<, <=<. >=)
4. The precondition for pointer relational operations is to point to elements in the same array at the same time
5. Comparisons between any two pointers (==,!=) are unlimited
6. The pointer types involved in the comparison operation must be the same

Applications of pointer operations:
The bounding position of the array, the position of the last element of the array element, which is legally syntactical

#include <stdio.h>

#define DIM(a) (sizeof(a) / sizeof(*a))

int main()
    char s[] = {'H','e','l','l','o','w'};
    char* pBegin = s;
    char* pEnd = pBegin + DIM(s);
    char* p = NULL;
    printf("pBegin = %p\n", pBegin);
    printf("pBegin = %p\n", pEnd);
    printf("size = %d\n", pEnd  - pBegin);

    for(p = pBegin; p < pEnd; p++)
        printf("%c", *p);
    return 0;

Subscript form vs pointer form
(1) When the pointer moves through the array in a fixed increment, it is more efficient than the subscript
(2) When the pointer increment is 1 and the hardware has a hardware increment model, it is more efficient
(3) Conversion between subscript form and pointer form
a[n] == *(a+n)
*(n+a) == n[a];
Arrays can be used as constant pointers or as array names

int a[] = {1,2,3,4,5};  //Address 0x601040 for a
extern int a[];	//Correct
extern int* a;		//Segmentation fault

#include <stdio.h>

int main()
    extern int* a;  //Simply declare
    printf("&a = %p\n", &a); //The address of a, to the address value of the identifier a
    printf("a = %p\n", a);		//A is a pointer variable, saves the address value, and then takes four bytes from the address 0x601040, or 0001, and prints out 0x1 (a pointer, so takes four bytes from the corresponding address of the pointer)
    printf("*a = %d\n", *a);	//The 0x1 address is reserved for use by the operating system and a segment error occurs when the user accesses the value of this address
    return 0;

The difference between a and a:
a is the address of the first element of the array
&a is the address of the entire array
The difference between a and a is pointer operations

a+1  ==  (unsigned int)a + sizeof(*a)
&a+1  ==  (unsigned int)(&a) + sizeof(*&a)  ==  (unsigned int)(&a) + sizeof(a)
#include <stdio.h>

int main()
    int a[5] = {1, 2, 3, 4, 5};
    int* p1 = (int*)(&a + 1); 
    int* p2 = (int*)((int)a + 1);
    int* p3 = (int*)(a + 1);
    printf("%d, %d, %d\n", p1[-1], p2[0], p3[1]);
    return 0;

0x0200000000 decimal = 33554432

The linux operating system is in small-end mode

Array parameters
When an array is a function parameter, the compiler compiles it into a pointer

void f(int a[]); ==  void f(int* a)
void f(int a[5]); ==  void f(int* a)
#include <stdio.h>

void func1(char a[5])
    printf("In func1: sizeof(a) = %d\n", sizeof(a));
    *a = 'a';
    a = NULL;

void func2(char b[])
    printf("In func2: sizeof(b) = %d\n", sizeof(b));
    *b = 'b';
    b = NULL;

int main()
    char array[10] = {0};
    printf("array[0] = %c\n", array[0]);
    printf("array[0] = %c\n", array[0]);
    return 0;

Generally, when you define an array parameter in a function, you need to define another parameter to represent the size of the array
(1) Array names and pointers are used in the same way only
Array names are not pointers, pointers are not arrays
(2) The array name is not the address of the array, but the address of the first element of the array
(3) Array parameters of functions degenerate to pointers

Posted on Sun, 07 Nov 2021 11:14:49 -0500 by rthconsultants