• Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

2. Array pointer/pointer array

Array Pointers are:

A pointer to an array, which is essentially a pointer, just like a pointer

Pointer arrays are:

An array of Pointers is essentially an array, just like character arrays or integer arrays

2.1 Understanding arrays

An array is essentially just a series of memory Spaces carved out by the compiler

The variable that represents the array is just the memory address of the first element in the sequence.

So when you show a compiler an array, it doesn’t see the whole array like a human would, it only sees the first element of the array, and it knows its memory address

Take a look at this code:

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int a[] = {1.2.3.4};     // An array
    printf("A memory address %p\na[0] memory address %p", &a, &a[0]);
    return 0;
}

Copy the code

His results are as follows:

Memory address of a0061FEC0
a[0] memory address0061FEC0
Copy the code

I’m sure you have a better understanding of arrays.

2.2 Array types and subscripts are enforced

So why do you need to enforce types to define arrays?

In the case of an int, the int takes four bytes

The position of the element in people’s eyes is +1

Equivalent to +4 in the compiler’s eyes (4 is the number of bytes consumed by the type)

So you can get the element exactly

How do we define array subscripts? Why do subscripts start at 0

Array subscripts are also obtained by adding and subtracting memory addresses

Because the compiler only remembers the memory address of the first element of the array

The subscript is the memory of the first element + I (I is the subscript)

The process of getting elements by subscript can be analogous to:

  • arr[1] => *(&arr +1)

The memory address is submarked, and the element is retrieved through a pointer

2.3 Array Pointers

An array pointer is a pointer to the first element of an array, as you can quickly understand from reading 2.1 and 2.2

Define an array pointer

int a[] = {1.3.5.7};     // An array
int (*p)[4] = &a;   // Define a pointer to the head element of the array
Copy the code

Access the second array element via a pointer:

printf("Access the second element of array: %d", *(*p+1));
Copy the code

Complete code:

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int a[] = {1.3.5.7};     // An array
    int (*p)[4] = &a;   // Define a pointer to the head element of the array
    printf("A memory address %p\na[0] memory address %p\n", &a, &a[0]);
    printf("Access the second element of array: %d", *(*p+1));
    return 0;
}
Copy the code

Running results:

Memory address of a0061FEBC
a[0] memory address0061FEBC accesses the second element of the array:3
Copy the code

2.4 Pointer Array

Pointer array, as the name implies, he is an array, just as often said character array, integer array, but pointer array definition method and store objects are also a billion points different.

Define an array of Pointers (for example, integers)

int *pArr[10];	// Define an array of Pointers
Copy the code

Note the distinction with the array pointer definition

Array pointer definition:

int (*arrP)[10];
Copy the code

It’s important to note the parentheses, because this is the priority of the * symbol, and if you write it wrong, it’s two different things.

Simple use:

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int *arr[10];		// Define an array of Pointers
    int arrSize = 10;	// The length of the pointer array
    for (int i = 0; i < arrSize; i++)
    {
        arr[i] = &i;	// Put the temporary address in the pointer array
        printf("Array element: %p\n The element to which the array element points %d\n", *arr[i]);
        /* code */
    }
    /* code */
    return 0;
}
Copy the code

Output result:

Array elements:0061FThe element to which the EA0 array element points0Array elements:0061FThe element to which the EA0 array element points1Array elements:0061FThe element to which the EA0 array element points2Array elements:0061FThe element to which the EA0 array element points3Array elements:0061FThe element to which the EA0 array element points4Array elements:0061FThe element to which the EA0 array element points5Array elements:0061FThe element to which the EA0 array element points6Array elements:0061FThe element to which the EA0 array element points7Array elements:0061FThe element to which the EA0 array element points8Array elements:0061FThe element to which the EA0 array element points9
Copy the code

Because I is a temporary variable, it is destroyed after each loop and opened again the next time it is used, so the memory address is the same.