Pointer array and pointer to pointer

Source: Internet
Author: User

Today I finally figured out the difference between a pointer array and a pointer pointing to a pointer.

 

1 # include <stdio. h>
2 # include <string. h>
3 void main ()
4 {
5
6 char a [] = "1234567890 MMMMMMM ";
7 char * p =;
8 char ** argv = & p;
9 printf ("% s \ n", * (argv + 0 ));
10 // char * argv [] = {"12345", "67890", "MMMMMMM "};
11 // for (I = 0; I <3; I ++)
12 //{
13 // printf ("% s \ n", argv [I]);
14 //}
15} I. pointer concept:
When our program declares the following variables:
Short int I;
Char;
Short int * pi;
The program will open up space for each variable in an address space in the memory, as shown in.
Memory Address → 6 7 8 9 10 11 12 13 14 15
Bytes -------------------------------------------------------------------------------------
... |
Bytes -------------------------------------------------------------------------------------
| Short int I | char a | short int * pi |
As shown in the figure, we can see:
The I variable occupies two bytes at the memory address 5.
A variable occupies one byte at the location of memory address 7.
The pi variable occupies two bytes at the memory address 9. (Note: pi is a pointer. Here, the pointer width is only two bytes, and the 32-bit system is four bytes)
Next, assign values as follows:
I = 50;
Pi = & I;
After the assignment in the preceding two sentences, the memory image of the variable is as follows:
Memory Address → 6 7 8 9 10 11 12 13 14 15
Bytes --------------------------------------------------------------------------------------
... | 50 | 6 |
Bytes --------------------------------------------------------------------------------------
| Short int I | char a | short int * pi |
No: the pi value of the Short integer pointer variable is 6, which is the memory start address of the I variable. Therefore, when we perform read/write operations on * pi, it is actually a read/write operation on the I variable. For example:
* Pi = 5; // It is equivalent to I = 5;

2. pointer address and pointer pointing to another pointer address
We can see that the pointer variable itself andOthersVariables are also in a memory address, for example, pi's memory start address is 10. Similarly, we may also point a pointer to this address.
See the following code:
Short int ** ppi; // This is a pointer to the pointer. Note that there are two * numbers.
Ppi = pi
First sentence: short int ** ppi; -- declares a pointer variable ppi, which is used to store (or point to) the address of a short int * type pointer variable.
The second sentence: & pi is the pi address, and ppi = pi is the address assigned to the ppi. Assign the address value 10 to the ppi. For example:
Memory Address → 6 7 8 9 10 11 12 13 14 15
Bytes ------------------------------------------------------------------------------------
... | 50 | 6 | 10 |
Bytes ------------------------------------------------------------------------------------
| Short int I | char a | short int * pi | short int ** ppi |
We can see that the starting address of the pointer variable pi is the content of the pointer variable ppi. So ......
What is the ppi value? -- 10.
* What is the ppi value? -- 6, that is, the value of pi.
** What is the ppi value? -- 50, that is, the value of I, is also the value of * pi.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.