c/c++ pointers #2

Table of Contents

Table of Contents

1: Arrays and pointers

1.1: Understanding array names

1.2: Using pointers to access arrays

1.3: The essence of array parameter passing

2: Secondary pointer

3: Pointer array

4: Array pointer

5: Character pointer

6: Function pointer

7: Array of function pointers


1: Array and pointer

Definition:

Pointer variables are pointer variables. The size of the pointer variable is either 8 bytes or 4 bytes according to different compilation environments. The purpose of the pointer variable is to store addresses.

An array is an array, not a pointer. The function of an array is to open up a continuous space in memory, which can be used to store one or more data of the same nature.

Contact:

The array name is actually the address of the array element. The array name <----address----> pointer, so the array can be accessed through the pointer.

1.1: Understanding of array names

It can be concluded from the compiler that the array name, the address of the first element of the array and the address of the array itself all point to the same address, then this What is the difference between the 3 types?

But there are two exceptions:

1: sizeof (array name): The array name here is not the address of the first element of the array. The array represents the entire array. The result calculated by sizeof is the size of the entire array, in bytes.

2: & amp; Array name, the array name here represents the entire array, and the address of the entire array is taken out through the & amp; address operator.

We add + 1 to each address in the above code

Why is this + 1 inconsistent? Refer to the array pointer section.

1.2: Use pointers to access arrays

1.3: The essence of array parameter passing

2: Secondary pointer

3: Pointer array

Referring to the meaning of integer array, character array, etc., then the essence of pointer array is array, but the data stored in it are all pointers.

Analogy: Integer array: an array that stores integers

Character array: an array that stores characters

Pointer array: an array that stores pointers

int main() {
int arr1[] = {1,2,3,4,5};
int arr2[] = { 6,3,1,9,10 };
int arr3[] = {24,55,23,11,33};

int* parr[] = { arr1, arr2, arr3 };// parr is a pointer array, which stores all pointers
for (int i = 0; i < 3; i + + ) {
for (int j = 0; j < 5; j + + ) {
printf("%d ", parr[i][j]);
}
printf("\\
");
}

return 0;
}

4: Array pointer

First of all, array pointers are pointers. Next, the analogy:

Integer pointer: (pointing to angle) pointer to integer variable, (storing angle) this pointer stores the address of the integer variable.

Character pointer: (pointing to angle) pointer to character variable, (storage angle) this pointer stores the address of character variable.

Array pointer: (pointing to the angle) pointer to the array, (storing the angle) is a pointer variable that stores the array address.

int *p1[10]; —-> Pointer array, because the priority of [ ] is greater than *, the variable p is first combined with [ ] —–> p[10], this is an array , 10 pointers or addresses are stored in the array.

int (*p2)[10]: array pointer, because the () number has the highest priority, so the * in the brackets is combined with p2 first—-> *p2, this is a pointer, and then this pointer points to a Array

Analysis of array pointers:

int (*p1)[100]= & amp; arr;

| | |

| | The p1 pointer points to the number of data in the array

| |

| | p1 is the array pointer variable name

|

| p1 points to the data type in the array

# Review of the case of &arr + 1 in the understanding of array names in 1.1. This is actually the + 1 operation on the array pointer, and then the entire array will be skipped.

Usage scenarios of array pointers: Generally, it is more convenient to use array pointers when operating two-dimensional arrays.

A two-dimensional array can be regarded as an array in which each element is a one-dimensional array, so each element in the two-dimensional array is a one-dimensional array. For a two-dimensional array, the first element In fact, it is the first line of address.

5: character pointer

There are two commonly used ways to initialize characters, one is to create a character array, and the other is to create a character pointer.

Extend it a bit and analyze the differences and connections in the following code:

int main() {
char str1[] = "subaru wrx sti";
char str2[] = "subaru wrx sti";
const char* str3 = "22b";
const char* str4 = "22b";

if (str1 == str2) {
printf("str1==str2\\
");
}
else {
printf("str1!=str2\\
");

}
if (str3 == str4) {
printf("str3==str4\\
");
}
else {
printf("str3!=str4\\
");

}
return 0;
}

6: Function pointer

Functions actually have addresses. Just like arrays, we can also get the address of the function through the & amp; address operator.

However, the function address and the array address are still slightly different in whether the & amp; operator is added.

Array Function
Array name: Indicates the address of the first element of the array Function name: Function address
&Array name: Represents the address of the entire array &Function name: function address

Declaration form of function pointer:

int (*pf) (int x, int y);

| | |

| | |

| | pf points to the parameters and number of the function

| Function pointer variable name, please note that it must be quoted with ()

| The return value type of the function pointed to by the function pointer pf

A simple comparison:

7: Array of function pointers

Array of function pointers