Summary of Pointers

First-class pointer: Can be associated with the following

1. The variable

int a = 10

int *p = &a;

2. One-dimensional arrays

char a[10];

char *p = a;

3. Two-dimensional arrays

int a[3][2];

char *p =&a[0][0];

char *p = a[0];

Secondary pointer: Can be associated with the following

1. First-class pointer

int a = 10;

char *p = &a;

char **q = &p;

2. Pointer array

char *p[3];

char **q;

q = p;

Pointer array: Can be associated with the following

1. Secondary pointer

char *p[3];

char **q;

q = p;

2. Two-dimensional arrays

Char a [3] [2] = {6};

char *p[3];

P = a error

p[0] = a[0];

p[1] = a[1];

p[2] = a[2];

You can access the pointer array as if it were a two-dimensional array

Array pointer:

1. Two-dimensional arrays

char a[3][2];

char (*p)[2];

p = a;

[1] The relationship between a pointer and a one-dimensional array

char *p;
int *t;
char arr[10];
char trr[10];
Copy the code

p = arr; p = &arr[0]; //p is a pointer variable and arr is a constant

t = trr; t = &trr[0];

P is a pointer variable and arr is a constant (πŸ“’arr=p is incorrect)

πŸ“Œ If the pointer type is the same, and the pointer increments by 1 are moved by the same size. (arr+1 moves by the same amount as p+1).

*&arr = arr;

**&arr = *arr; // A one-dimensional array is equivalent to a two-dimensional array, plus two * to fetch the contents

β–Œ How to get pointer type: printf(“%d,arr”); Printf (“%d,p”); // The compiler makes an error and displays the type. // The type is char *

*arr     ===>arr[0] *arr++ ===> error/ / constant
(*arr)++ ===>arr[0] + +; *p ===>arr[0]
*p++     ===>arr[0] ,p++
(*p)++   ===>arr[0] + +;Copy the code

πŸ“ arr [I] < = = > * (arr + I) < = = > * (& arr [0] + I) < = = = > [I] p < = = > * (p + I) < = = > * (& p + I) [0]

// Pointer +1, move an element, address +1, in memory array contiguous, so equal sign

[2] The relationship between a pointer and a two-dimensional array

int a[3][2];
int *p;
Copy the code
  • // P and a have different pointer types
  • // P +1 moves 4 bytes, a+1 moves 8 bytes
  • // Therefore, it is not possible to draw an equal sign between the two
  • //p = a; Incorrect assignment method

P = (int *)a; p = &a[0][0];

p = a[0];

Access the members of an array through Pointers

  • *p++; //p refers to the first address of the two-dimensional array

  • *(p+i); //p refers to the first address of the two-dimensional array, p+ I, which element’s address, * takes the value in the address

  • p[i]; = = > p + I; //p points to the first address of a two-dimensional array. A one-level pointer is equivalent to a one-dimensional array.

πŸ“’! Note: p[I][j] is incorrect //p[I] is already a value, a first-order pointer is equivalent to a one-dimensional array

Sample code for accessing members of an array through Pointers

#include <stdio.h>
#define ARRAY_SIZE(res) (sizeof(res)/sizeof(res[0][0]))
int main(int argc, const char *argv[])
{
    int i;
    int a[3] [2] = {1.2.3.4.5.6};
    int *p;
	
    //p = a;
    //printf("%d\n",p);
    //printf("%d\n",a);
    //p = &a[0][0];
    printf("%d\n",a[0]);
    p = a[0];	
    for(i=0; i<ARRAY_SIZE(a); i++){printf("%d\t",*p++);
}
    puts("");
    puts("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
    p = &a[0] [0];// notice here that the value is reassigned each time, because the pointer moves (p++)

for(i=0; i<ARRAY_SIZE(a); i++){printf("%d\t",*(p+i));
}
puts("");
puts("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");

p = &a[0] [0];
for(i=0; i<ARRAY_SIZE(a); i++){printf("%d\t",p[i]);
}
puts("");
puts("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");

return 0;
}
Copy the code

Members are accessed by the array name

  • A [I][j] //
  • *(a[I]+j)//a[I]: = (a[I]+j)//a[I]: = (a[I]+j
  • ((a+ I)+j)//a is the first address of the array,(a+ I) represents the address of the line //(a+ I)+j: represents the address of the ith row and the JTH column //((a + I + j) : / / a value from this address is a two dimensional array, front take * is equivalent to one dimensional array
  • (&a[0][0]+(i2)+j)//&a[0][0] // the address of the 0th element. The 2 here is the number of columns when the exponential group is defined

(*(a+i))[j]

πŸ“Œ line [two-dimensional]----{*or[]}---- column [one-dimensional]----*orThe []---- element & cancels with [] * is equivalent to []Copy the code