The essence of C++ pointer parameter transmission and how to use the secondary pointer
This article mainly introduces the C++ pointer parameter transmission essence and how to use the secondary pointer related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this C++ pointer parameter transmission essence and how to use the secondary pointer article will have a harvest, let's take a look.
Let's first look at two programs:
1:
Void test (char * p)
{
Printf ("[test1] [p]:% p.\ n", p)
Printf ("[test2] [p]:% s.\ n", p)
P = (char *) malloc (10)
Strcpy (p, "ABCDE")
Printf ("[test3] malloc after.\ n")
Printf ("[test4] [p]:% p.\ n", p)
Printf ("[test5] [p]:% s.\ n", p)
Free (p)
}
Int main ()
{
Char b [6] = "abcde"
Char * a = b
Printf ("[main1] [a]:% p.\ n", a)
Printf ("[main2] [a]:% s.\ n", a)
Test (a)
Printf ("[main3] [a]:% p.\ n", a)
Printf ("[main4] [a]:% s.\ n", a)
Return 0
}
Output: note: (the pde value of the test function has changed, but the value of an of the main function has not changed)
Main1] [a]: 0xbfeaaef6.
[main2] [a]: abcde.
[test1] [p]: 0xbfeaaef6.
[test2] [p]: abcde.
[test3] after malloc.
[test4] [p]: 0x8a52008.
[test5] [p]: ABCDE.
[main3] [a]: 0xbfeaaef6.
[main4] [a]: abcde.
2:
Void test (char * * p)
{
Printf ("[test1] [p]:% p.\ n", p)
Printf ("[test2] [* p]:% p.\ n", * p)
* p = (char *) malloc (10)
Strcpy (* p, "ABCDE")
Printf ("[test3] malloc after.\ n")
Printf ("[test4] [p]:% p.\ n", p)
Printf ("[test5] [* p]:% p.\ n", * p)
Printf ("[test6] [* p]:% s.\ n", * p)
Free (* p)
}
Int main ()
{
Char b [6] = "abcde"
Char * a = b
Printf ("[main1] [a]:% p.\ n", a)
Printf ("[main2] [a]:% s.\ n", a)
Test & a)
Printf ("[main3] [a]:% p.\ n", a)
Printf ("[main4] [a]:% s.\ n", a)
Return 0
}
Output: note: (the pde value of the test function has changed, and the value of an of the main function has also changed)
[main1] [a]: 0xbfaca776.
[main2] [a]: abcde.
[test1] [p]: 0xbfaca770.
[test2] [* p]: 0xbfaca776.
[test3] after malloc.
[test4] [p]: 0xbfaca770.
[test5] [* p]: 0x9132008.
[test6] [* p]: ABCDE.
[main3] [a]: 0x9132008.
[main4] [a]: ABCDE.
This is the end of the article on "the essence of C++ pointer parameter transmission and how to use the secondary pointer". Thank you for reading! I believe that everyone has a certain understanding of the "C++ pointer parameter transmission essence and how to use the secondary pointer" knowledge, if you want to learn more knowledge, welcome to follow the industry information channel.