Advanced Usage of C Language_(11)_Pointers

Table of contents

1 Basic concepts of pointers 

2 secondary pointer 

3 Arithmetic operations on pointers

4 void pointers

5 const pointers

6 Pointer arrays and array pointers

1 array of pointers

2 array pointers

​7 The relationship between pointers and arrays

 1 The relationship between pointers and one-dimensional arrays

 2 The relationship between pointers and two-dimensional arrays

 3 Ways to access array elements


1 Basic concepts of pointers 

1 address: the number used to distinguish different bytes in memory

2 pointer: the address is the pointer, the pointer is the address

3 pointer variable: a variable that stores an address

4 &: This symbol is used to obtain the first address of a variable in the memory space, and to upgrade the expression type. For example int type--->>int* type.

5 *: If this symbol is on the right side of the expression =, it means to take the value of the space pointed to by the pointer (the size of the space obtained depends on the type of the pointer). If it is on the left of the expression =, it means that the value on the right is put into the space pointed to by the pointer. Also has the effect of downgrading the expression type. For example int* type--->>int type.

As shown in the figure below, four bytes are used to store the integer 100, and eight bytes are used to store the pointer to the first address of the integer variable.

2 secondary pointer 

We call a pointer a variable that stores the address of a variable, but what about pointers that we want to store pointers to? Then use the secondary pointer.

int number = 0;

int *pnum = #

int **ppnum = &pnum;                                                                                                               

Here is a program to explain

#include <stdio.h>

int main(int argc, char const *argv[])
{
          int num = 100;
          int *pnum = &num;
          int **ppnum = &pnum;

          printf("num = %d\n",num);
          printf("*pnum = %d\n",*pnum);
          printf("**ppnum = %d\n",**ppnum);
          printf("&num = %p\n",&num);
          printf("pnum = %p\n",pnum);
          printf("*ppnum = %p\n",*ppnum);
          printf("&pnum = %p\n",&pnum);
          printf("ppnum = %p\n",ppnum); 
        
          return 0;
}

 

However, secondary pointers are used in two places in C:

 1 When the function body wants to modify the value of the pointer variable outside the function body, the address of the pointer variable is the second-level pointer.

 2 When passing parameters to a pointer array, the array name of the array is a pointer to the first pointer element of the array.

3 Arithmetic operations on pointers

+         -         ++          --

The pointer offset is the size of the pointed data type byte space

int* :4

char* :1

double* :8

int** :8

4 void pointers

void *p;

It is often used to point to a storage memory address, and has no meaning of pointing to the size of the space.

Pointers of void* type should not be used for operations related to fetching * or ++ --.

The conversion between void * type pointer and other types of pointers does not require mandatory type conversion.

use

Used as a function parameter, or a function return value to represent a pointer compatible with all types.

1 memcpy (void *dest, const void *src, size_t n); parameter definition of memory copy function

2 memcmp(const void *s1,const void *s2,size_t n);

An example question: assign an integer 100 to the space whose memory address is 0x2000 (0x2000 is an address of void * type)

           (*(int *)((void*)0x2000)) = 100; or (* (int *)0x2000) = 100;

5 const pointers

1. const int *p;

2. int const *p;

3. int *const p;

4. const int *const p;

5. int const *const p;

1 and 2 are equivalent, const is modified by *p, and the space pointed to by p cannot be changed (read-only), but p can be modified. (such as the const char *src parameter of the strcpy function, and we cannot modify the source). It can be used but cannot be changed.

In 3, the const is modified by p, and p cannot be changed, but *p can be changed, so it must be initialized, because in the program, p can only point to the space at the time of initialization, and cannot point to other spaces, but the pointer can be used to modify the space pointed to by p value. (For example, the array name of the array always points to the first address of the first element, and we can modify the value of the first element).

4 and 5 are equivalent, const is modified by p and *p, and the address and the space pointed to by the pointer cannot be changed. Must be initialized.

6 Pointer arrays and array pointers

1 array of pointers

Array of pointers:

An array of pointers is an array, that is, an array of pointers, and each element in this array is a pointer.

int *p[5];

For example, above, an array p is defined, and there are five elements in the array, each element is 8 bytes, a total of 40 bytes, and each element is a pointer to an integer variable.

Integer pointer arrays are generally used less, and character pointer arrays (char *pstr[5]) are generally used in C language

code to understand

#include <stdio.h>

int main(int argc, char const *argv[])
{
          char *pstr[5] = {"hello","world","how","are","you",};
          for(int i = 0;i < 5;i++)
          {
                    printf("str[%d] = %s\n",i,pstr[i]);
          }
          return 0;
}

In general, a two-dimensional array is used to store strings, and an array of pointers is used to manipulate strings.

For example, we use bubble sort to sort an array of strings, where addresses are exchanged instead of strings.

As shown in the example code below

in(int argc, char const *argv[])
{
          char *pstr[5] = {"hello","world","how","are","you",};
          int i = 0;
          int j = 0;
          char *ptmp = NULL;
          for(i = 0;i < 5 -1;i++)
          {
                    for(j = 0;j < 5 -1 - i;j ++)
                    {
                              if(strcmp(pstr[j],pstr[j + 1]) > 0)
                              {
                                        ptmp = pstr[j];
                                        pstr[j] = pstr[j + 1];
                                        pstr[j +1] = ptmp;
                              }
                    }
          }

          for(i = 0; i < 5; i++)
          {
                    printf("%s\n",pstr[i]);
          }
          return 0;
}

2 array pointers

 The array pointer is a pointer, the pointer points to the array, and the pointer occupies eight bytes.

int (*a)[5];

int a[5];

&a:int (*)[5]

1 For the one-dimensional array array name & operation, the value-invariant type is upgraded to an array pointer.

2 For the * operation on the array pointer, the value invariant type is downgraded to a pointer to the first element of the array

Explain the code

#include <stdio.h>

int main(int argc, char const *argv[])
{
          int (*p)[5] = NULL;
          int a[5] = {1,2,3,4,5,};
          p = &a;

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

 7 The relationship between pointers and arrays

 1 The relationship between pointers and one-dimensional arrays

 int a[5] = {1,2,3,4,5};

The array-name of an array is a pointer to the first element of the array.

a = &a[0]

The way to access array elements: a[n] = *(a + n) 

 2 The relationship between pointers and two-dimensional arrays

The array name a of the two-dimensional array is an array pointer pointing to the elements of the first row of the array.

#include <stdio.h>

int main(int argc, char const *argv[])
{
          int a[2][3] = {1,2,3,4,5,6};

          printf("================================\n");
          printf("&a[0][0] = %p\n",&a[0][0]);
          printf("&a[0][1] = %p\n",&a[0][1]);
          printf("&a[0][2] = %p\n",&a[0][2]);
          printf("&a[1][0] = %p\n",&a[1][0]);
          printf("&a[1][1] = %p\n",&a[1][1]);
          printf("&a[1][2] = %p\n",&a[1][2]);
          printf("================================\n");
          printf("a = %p\n",a);
          printf("a + 1 = %p\n",a + 1);
          printf("================================\n");
          printf("a[0] = %p\n",a[0]);
          printf("a[0] + 1 = %p\n",a[0] + 1);
          printf("a[1] = %p\n",a[1]);
          printf("a[1] + 1 = %p\n",a[1] + 1);
          printf("================================\n");
          printf("*a[0] = %d\n",*a[0]);
          printf("*(a[0] + 1)= %d\n",*(a[0] + 1));
          printf("*a[1] = %d\n",*a[1]);
          printf("*(a[1] + 1) = %d\n",*(a[1] + 1));
          printf("================================\n");
          printf("*a = %p\n",*a);
          printf("*(a + 1) = %p\n",*(a + 1));
          return 0;
}

 

 3 Ways to access array elements

a[m][n] = *(a[m] + n) = *(*(a + m) + n)

*(p + m * N + n)

Guess you like

Origin blog.csdn.net/m0_58193842/article/details/128369481