pointer to pointer in c language

1. Situation

The pointer of the c language pointer is still a commonly used function; of course, I also believe that some people who have used the C language for a long time have not used it much, because it is not used, which is determined by work requirements, but overall That being said, it is still used frequently.
After understanding the pointer of the pointer, I feel that I really understand the meaning of the pointer

2. Definition

A pointer to a pointer is a form of multi-level indirection, or a chain of pointers. Usually, a pointer contains the address of a variable. 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 in C
A pointer to pointer variable must be declared by placing two asterisks before the variable name. For example, the following declares a pointer to a pointer of type int:
int **var;


 3. Instances of failure


1 #include <stdio.h>
  2 #include <string.h>
  3 #include <memory.h>
  4 #include <stdlib.h>
  5 
  6 void getMemory(char *p, int num)
  7 {
  8  printf("enter function getMemory\r\n");
  9  printf("p=%p,&p=%p\r\n", p, &p);
 10  p = (char *)malloc(sizeof(char) * num);
 11  printf("p=%p,&p=%p\r\n", p, &p);
 12  printf("exit function getMemory\r\n");
 13 }
 14 
 15 int main()
 16 {
 17   char *str = NULL;
 18   printf("str=%p,&str=%p\r\n", str, &str);
 19   getMemory(str, 100);
 20   strcpy(str, "hello");
 21   printf("str=%s\r\n", str);
 22   printf("str=%p\r\n", str);
 23   printf("&str=%p\r\n", &str);
 24   free(str);
 25 
 26 }
~                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                              
"test2.c" 26L, 538C 已写入             

root@mkx:~/learn/getMemory# ./test2
str=(nil),&str=0x7ffd24ae73c0
enter function getMemory
p=(nil),&p=0x7ffd24ae73a8
p=0x6a9420,&p=0x7ffd24ae73a8
exit function getMemory
段错误 (核心已转储)
root@mkx:~/learn/getMemory# 

4. Successful examples


 1 #include <stdio.h>
  2 #include <string.h>
  3 #include <memory.h>
  4 #include <stdlib.h>
  5 
  6 void getMemory(char **p, int num)
  7 {
  8  printf("enter function getMemory\r\n");
  9  printf("p=%p,*p=%p\r\n", p, *p);
 10  *p = (char *)malloc(sizeof(char) * num);
 11  printf("p=%p,*p=%p\r\n", p, *p);
 12  printf("exit function getMemory\r\n");
 13 }
 14 
 15 int main()
 16 {
 17   char *str = NULL;
 18   printf("str=%p, &str=%p\r\n", str, &str);
 19   getMemory(&str, 100);
 20   strcpy(str, "hello");
 21   printf("str=%s\r\n", str);
 22   printf("str=%p\r\n", str);
 23   printf("&str=%p\r\n", &str);
 24   free(str);
 25 
 26 }
~                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                              
"test1.c" 26L, 542C            
root@mkx:~/learn/getMemory# gcc test1.c -o test1
root@mkx:~/learn/getMemory# ./test1 
str=(nil), &str=0x7ffeddf9e010
enter function getMemory
p=0x7ffeddf9e010,*p=(nil)
p=0x7ffeddf9e010,*p=0xf22420
exit function getMemory
str=hello
str=0xf22420
&str=0x7ffeddf9e010

 5. Final summary

The case of the failing example is this:

Failure is failure. The address of the variable p passed to the function parameter is different from the address of the current variable str. They only store the same content. This determines the operations on both sides, which has nothing to do with it. Later, Assigning a value to a variable that has no memory allocated, the program must crash

A successful example situation is this:

The success here lies in the use of pointer pointers. When I think about it, I feel a little confused. Think about it carefully. The address of the allocated memory is assigned, it's that simple.

Guess you like

Origin blog.csdn.net/maokexu123/article/details/126309733