What is the pointer in C language?


foreword

While pointers simplify the performance of some C programming tasks, others, such as dynamic memory allocation, cannot be performed without pointers.
Therefore, if you want to become an excellent C programmer, it is necessary to learn pointers.


1. What is a pointer?

A pointer is a variable that stores a memory address.
This memory address points to the location of another variable, enabling the program to access and manipulate the value of that variable.

Every variable has a memory address, and the memory address is 可用 & 运算符取到内存地址.

Pointer declarations use the * symbol

// p 是一个指针变量,它存储了一个地址
// *p 是指针 p 所指向的地址上存储的值
int *p;

In this statement, the variable p is a pointer of integer type. You can use the & symbol to get the address of a variable.

int n = 10;
int *p = &n;

In this statement, &n gets the address of variable n and assigns the address to pointer p. Through the pointer p, the value of the variable n can be accessed and modified.

*p = 20;
printf("%d", n); // 输出20

*In this statement, p represents the value stored in the memory address pointed to by the pointer p. After assigning *p with a value of 20, the value of variable n is also changed to 20.
insert image description here

Second, how to use the pointer?

Define a pointer variable;
assign the variable address to the pointer;
access the value of the address available in the pointer variable.
These are returned by using the unary operator * to return the value of the variable at the address specified by the operand.

1. Arithmetic operations on pointers

  • Each time the pointer 递增points to 下一个the storage unit of the element.
  • Each time the pointer 递减points to 前一个the storage unit of the element.
  • The number of bytes that the pointer jumps when incrementing and decrementing depends on 指针变量数据类型长度, for example, an int is 4 bytes.

increment a pointer

#include <stdio.h>

const int MAX = 3;

int main ()
{
    
    
   int  var[] = {
    
    10, 100, 200};
   int  i, *ptr;
   /* 指针中的数组地址 */
   ptr = var;
   for ( i = 0; i < MAX; i++)
   {
    
    
      printf("存储地址:var[%d] = %p\n", i, ptr );
      printf("存储值:var[%d] = %d\n", i, *ptr );
      /* 指向下一个位置 */
      ptr++;
   }
   return 0;
}
// 存储地址:var[0] = e4a298cc
// 存储值:var[0] = 10
// 存储地址:var[1] = e4a298d0
// 存储值:var[1] = 100
// 存储地址:var[2] = e4a298d4
// 存储值:var[2] = 200

Comparison of pointers
As long as the address pointed to by the variable pointer is less than or equal to the address &var[MAX - 1] of the last element of the array, the variable pointer is incremented.

#include <stdio.h>
 
const int MAX = 3;
 
int main ()
{
    
    
   int  var[] = {
    
    10, 100, 200};
   int  i, *ptr;
 
   /* 指针中第一个元素的地址*/
   ptr = &var;
   i = 0;
   while ( ptr <= &var[MAX - 1] )
   {
    
    
      printf("存储地址:var[%d] = %p\n", i, ptr );
      printf("存储值:var[%d] = %d\n", i, *ptr );
 
      /* 指向下一个位置 */
      ptr++;
      i++;
   }
   return 0;
}

2. Array of pointers

ptr is declared as an array consisting of MAX integer pointers.
Therefore, each element in ptr is a pointer to an int value.

int *ptr[MAX];

The following example uses three integers, which will be stored in an array of pointers, as follows:

#include <stdio.h>
 
const int MAX = 3;
 
int main ()
{
    
    
   int  var[] = {
    
    10, 100, 200};
   int i, *ptr[MAX];
 
   for ( i = 0; i < MAX; i++)
   {
    
    
      ptr[i] = &var[i]; /* 赋值为整数的地址 */
   }
   return 0;
}

3. Pointer to Pointer

A pointer to a pointer is a form of multi-level indirection. When we define a pointer to pointer, the first pointer contains the address of the second pointer, which points to the location containing the actual value.
Pointer to pointer of type int:

int a = 100;
int *p1 = &a;
int **p2 = &p1;

insert image description here
The C language does not limit the number of levels of pointers. For each additional level of pointers, an asterisk * must be added when defining pointer variables.
p1 is a first-level pointer, pointing to common type data, with one * when defined;
p2 is a second-level pointer, pointing to first-level pointer p1, with two * when defined.

4. Pass the pointer to the function

#include <stdio.h>

void increment(int *ptr) {
    
    
    (*ptr)++;
}

int main() {
    
    
    int num = 5;
    printf("Before increment: %d\n", num);
    increment(&num);
    printf("After increment: %d\n", num);
    return 0;
}

5. Return pointer from function

// todo

Guess you like

Origin blog.csdn.net/weixin_42465316/article/details/129753765