Pointer parsing (2) (original)

Source: Internet
Author: User

Next, choose http://www.bkjia.com/kf/201202/121314.html. the answer is as follows:

1. What does & ch represent?

A: The address of the variable ch.

2. What does pch mean?

A: pch is a char pointer pointing to ch.

3. What does & pch represent?

A: Address of the pointer variable

4. * What does pch mean?

A: The pointer variable pch is unreferenced and the ch value is indirectly accessed.

5. * What does pch + 1 represent?

A: For indirect access operations, add one copy value (* pch + 1) to 'B '.

6. * (pch + 1) What does it mean? Ah,

A: Content on the next address after access to ch.

7. What does ++ pch mean?

A: If it is the right value, it should be incremental first and then transmitted to the address.

8. What does * ++ pch represent?

A: If it is the right value, it should increase first, and then indirectly access it through the address.

9. * What does pch ++ represent?

A: assume that the value is the right value. Then, it is accessed indirectly through the address, and then incremented.

10. ++ * What does pch represent?

A: add one to the value indicated by pch. It can be seen as this, ++ (* pch ).

11. What does (* pch) ++ represent?

A: If it is the right value, first pass the value pointed to by pch to the left value, and then increment the copied value.

12. What does ++ * ++ pch represent?

A: ++ (* ++ pch) is clear.

13. ++ * What does pch ++ represent?

A: ++ (* pch ++) can refer to the Ninth Question.

Now it is time to relate data to pointers. First read the following code:

Int a [10]; // defines an array of the int type int * pa with a length of 10; // defines an int type pointer pa =; or pa = & a [0];
The code above illustrates the following:

① The array name represents the address of the first element of the array.

② This is represented in graphs:

 

 

The above small content is just an appealer, and the real big meal will come soon !!
1. Character pointer
A String constant is a character array. For example, "I Love You! ", The character array ends with" \ 0. In this case, the array represents a String constant and a character pointer represents a great difference:
Char amessage [] = "I Love You! "; // Defines an array
Char * pmessage = "I Love You! "; // Defines a character pointer
Their differences are:

 
 

It seems that there is no big difference in this figure, that is, the pmessage has an arrow. They are actually quite different. Amessage always points to the same storage location. You can modify the letters in a String constant. Pmessage is just a char pointer, and its initial value points to a constant of the string. If you try to modify the content of the string, the result is not defined.
2. pointer operation
When performing arithmetic operations on the pointer and an integer, the integer is always adjusted to an appropriate size before the addition operation is performed.
①. Arithmetic Operations
Pointer ± integer
This applies to arrays and memory allocated using the malloc function. For example, the following code:
# Include <iostream>
 
Int main ()
{
Int values [5];
Int * vp;
For (vp = values; vp! = & Values [5];)
{
* Vp ++ = 0; // the pointer is incremented and assigned here.
}

For (vp = values; vp! = & Values [5];)
{
Std: cout <* vp ++ <std: endl;
}
 
Return 0;

}
Pointer-pointer
This Subtraction is meaningful only when both pointers point to the same array. For example, the following code:
# Include <iostream>
 
Int main ()
{
Int values [5];
Int * vp;
Int * vp1;
Int * VP2;
 
For (vp = values; vp <& values [5];)
{
* Vp = 0;
Vp ++;
}

Vp1. = values;
Http://dt.ap-southeast-1.maxcompute.aliyun-inc.com/api
Std: cout <(P0-VP2.) <std: endl; // displayed as-3, ptr_diff type
 
Return 0;

}
3. pointer array, array pointer, multi-dimensional array
What is a pointer array? In legend, an array with all array elements as pointers is called a pointer array. The one-dimensional pointer array is defined as "type name * array identifier [array length]". For example:
Char * ptr_array [10]; // pointer array, which contains 10 char-type pointers
// * Ptr_array [I]: the first character pointing to the I-th line of text
This is probably the case:

 

 

What is the difference between a pointer array and an array pointer? An array pointer is a pointer to the address of the first element of an array. In essence, it is a pointer. A pointer array is an array whose elements are pointers. The difference between the two is clear.
Multidimensional array and pointer array: take a two-dimensional array as an example.
Char ar [3] [8] = {"we", "are", "chinese "};
Char * parray [3] = {"we", "are", "chinese "};
This is probably the same:

 

It is obvious that multi-dimensional arrays differ greatly from pointer arrays. For a two-dimensional array: if a two-dimensional array is defined and no value is assigned, the system allocates the corresponding space to it, and the space must be continuous. Each element represents a single character. We can modify its elements by creating a subscript.
For pointer Arrays: for example, the char * parray [3] system allocates at least three consecutive spaces to store three elements, indicating that parray is an array of three elements, each element is a pointer to struct data.
In the compiler, ar [3] [8] is considered as * (ar + I) + j). It is regarded as a pointer.
4. Summarize the differences and relationships between arrays and pointers.
Differences:

 

Pointer Array
Indirect Data Access Direct access to data
Save address Save data
Dynamic Data Structure Fixed number of elements of the same type
Malloc (), free () Implicit Allocation Mechanism
Point to anonymous data Itself is Data
Contact:
1. the array name in the expression is treated as a pointer to the first element of the array by the compiler.
2. The subscript is always the same as the pointer offset.
3. In function parameters, the array name is used by the compiler as a pointer to the first element of the array.
4. Although multi-dimensional arrays are different from multi-level pointers, they are regarded as multi-level pointers in the compiler.
 

Let's talk about it! This article has been written for a long time and is not well written. please correct me. Thank you!

From cloud flying elephant cg
 

Related Article

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.