In depth understanding of the two USES of double Pointers
- 2020-04-02 01:02:24
- OfStack
I haven't used the second level pointer of C/C++ for a long time. I always feel that it is the pointer of the pointer. It's no big deal. So, from the Internet to find information, learned some......
The question is:
Q: can the program achieve its goal of creating a space in GetMemory () for the main function and pointing STR to that space?
Analysis: STR is a pointer to NULL, the parameter p is also a pointer to NULL, and in the GetMemory function, the pointer points to a new space. But only the parameter's pointing has changed, and the argument STR still points to NULL, not changed. Therefore, the program cannot meet the requirements of the topic, and there will be an error when running, because STR always points to NULL, when executing strcop, there will be an error, indicating that certain memory cannot be written.
The correct method should be double pointer, as follows:
Analysis: STR is a pointer to NULL. When the GetMemory function is called, the address of STR is passed, p is a secondary pointer, and *p is a pointer. Therefore, if the address of STR is assigned to the temporary variable p, *p is the value of the pointer STR, and changing the value of *p is the same as changing the value of STR. So this is the way to get what they want. In addition, there is another method, using a pointer, let the function return a pointer variable, pointing to the newly allocated memory, the program is as follows:
2. In addition, the second pointer is often used in the dynamic application of two-dimensional array.
This enables dynamic application of two-dimensional arrays, because subscripts are not allowed to be variables when arrays are declared, so it is best to do this if you want to dynamically determine the size of each dimension of an array.
Attachment: some definitions of Pointers
The question is:
#include "stdafx.h"
#include <iostream>
using namespace std;
void GetMemory(char *p, int num)
{
p = (char *)malloc(sizeof(char) * num);
//p = new char[num]; //C++ among
}
int _tmain(int argc, _TCHAR* argv[])
{
char *str = NULL;
GetMeory(str, 100);
strcpy(str,"Hello");
cout << str << endl;
return 0;
}
Q: can the program achieve its goal of creating a space in GetMemory () for the main function and pointing STR to that space?
Analysis: STR is a pointer to NULL, the parameter p is also a pointer to NULL, and in the GetMemory function, the pointer points to a new space. But only the parameter's pointing has changed, and the argument STR still points to NULL, not changed. Therefore, the program cannot meet the requirements of the topic, and there will be an error when running, because STR always points to NULL, when executing strcop, there will be an error, indicating that certain memory cannot be written.
The correct method should be double pointer, as follows:
#include "stdafx.h"
#include <iostream>
using namespace std;
void GetMeory(char **p, int num)
{
*p = (char *)malloc(sizeof(char) * num);
//*p = new char[num]; //C++ among
}
int _tmain(int argc, _TCHAR* argv[])
{
char *str = NULL;
GetMeory(&str, 100);
strcpy(str,"Hello");
cout << str << endl;
return 0;
}
Analysis: STR is a pointer to NULL. When the GetMemory function is called, the address of STR is passed, p is a secondary pointer, and *p is a pointer. Therefore, if the address of STR is assigned to the temporary variable p, *p is the value of the pointer STR, and changing the value of *p is the same as changing the value of STR. So this is the way to get what they want. In addition, there is another method, using a pointer, let the function return a pointer variable, pointing to the newly allocated memory, the program is as follows:
#include "stdafx.h"
#include <iostream>
using namespace std;
char * GetMeory2(char *p, int num)
{
p = (char *)malloc(sizeof(char) * num);
//p = new char[num]; //C++ among
return p;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *str = NULL;
str = GetMeory2(str, 100);
strcpy(str,"Hello");
cout << str << endl;
return 0;
}
2. In addition, the second pointer is often used in the dynamic application of two-dimensional array.
void main()
{
int m , n , **p;
scanf("%d%d" , &m , &n);
p = (int **)malloc(m * sizeof(int *))
//P = new int* [m];
for(i = 0 ; i < m ; i++)
p[i] = (int *)malloc(n * sizeof(int));
//C + + : p[i] = new int[n];
}
This enables dynamic application of two-dimensional arrays, because subscripts are not allowed to be variables when arrays are declared, so it is best to do this if you want to dynamically determine the size of each dimension of an array.
Attachment: some definitions of Pointers
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201305/201305290859422.jpg ">