The pointer of C + + starting from zero and its semantics and application

Source: Internet
Author: User
Tags arrays constant decimal to binary numeric modifier modifiers readable

This article is the "C + + from the beginning of the" series of the attached article. Because friends have repeatedly thought that the "C + + from the zero-start" series on the pointer to the exposition too simplistic, and put forward the concept of miscellaneous seven mixed eight, and about pointers in C + + important concepts in the use of less and less, so this article focuses on the "C + + from the start" series of numbers, addresses, pointers and other basic concepts, The semantics of pointers and the relationship between pointers and arrays are given, and the semantics of multilevel pointers, multidimensional arrays, function pointers, array pointers, member pointers, and their respective applications are described.

Number, operator, type, type modifier

In the "C + + from scratch (c)" has been explained that in fact, the CPU even binary number is not recognized, it can only deal with the state, and it can handle the state just can be used to express the binary number, it is said that the CPU only know the binary number. It should be noted that because the CPU is aware of the binary number is to recognize its expressed state, not in the mathematical sense of the binary number, So the CPU does not know the decimal number 20. However, by converting 20 to a binary number of 10100 by mathematical rules, good luck. The CPU's designer defines the addition instruction as state 10100 and state 10100 to add a state of 101000, and the binary number is converted to a decimal number by the mathematical rule, which is exactly 40, that is, CPU plus and minus Except that it will only change the state in a different way, and the CPU designers specifically define the changes in the state sing Woo the mathematical subtraction to make the CPU appear to be subtraction.

So, in order for the CPU to execute an instruction, then the instruction is involved in only binary numbers, it must have rules to give the mathematical meaning of the number into binary number. such as the previous decimal to binary rules, in the "C + + from the Beginning (ii)" mentioned in the original code, complement, IEEE real*4. and to write C + + code, you must be in the code to reflect the above conversion rules, and to be able to reflect the code to be converted in the mathematical sense of the number, so that the compiler can be based on the code we write to determine what the CPU to manipulate the binary representation. In this respect, C + + in the form of the former, with the number of the latter, using the operator to represent the CPU's instructions, that is, the transformation of the CPU state.

Therefore, in order for the CPU to perform the addition instruction, the operator on the code that writes the addition instruction--"+", "+" must have two digits on either side, and the type of the number determines how the mathematical number represented by the number is converted to binary numbers. Note that the number is a compile-level concept, not a code-level concept, that is, you cannot represent numbers on the code, but only through the return of the operator's calculations. Because any operator returns a number (an operator that does not return a number can be represented by a number that returns a type of void), the most common kind of operator that gets a number is what is usually called a constant, such as 6.3, 5.2f, 0772, and so on. What I call the number in C + + from scratch (ii) is that it does cause conceptual confusion, and this is especially clear.

It should be noted that as long as the return of the number of things is the operator, so the previous constant is also an operator. For variables, member variables, and functions, the C + + from the zero-based series has repeatedly emphasized that they are mapping elements, direct writing variable names, member variable names and function names to return their respective mapped numbers, that is, variable name function name, etc. are also operators.

Numbers are of type, C + + provides custom type struct, class, and so on, but not only that, but also C + + provides something more praiseworthy-type modifiers. In C + + zero-based (v), a type modifier is used to modify the numeric conversion rules represented by a modified type (called the original type) by a rule. such as pig blood sheep blood and pork mutton, where the "blood" and "meat" are type modifiers, change their original type-"pig" and "sheep." It feels more like the latter to modify the former than the former to modify the latter, such as pig blood in the "blood" is the subject and "pig" is the attribute. That is, the type modifier actually modifies the numeric conversion rule that it represents by the original type of information. This is called "blood", "meat" is a thing, also said that a type is a pointer type, reference type, array type, function type, and so on.

In the C + + zero-based series, the following types of modifiers are proposed--reference to "&", Pointer "*", array "[]", Function "()", Function call rule "__stdcall", offset < custom type name:: ", constant" const " and address type modifiers. The address type modifiers are the most confusing.

In C + + zero-based (iii) It has been stated that the address is a number in a 32-bit operating system, which is often represented by a 32-bit long binary number to uniquely identify a specific internal deposit. When the type of a number is an address type (because there is an address type modifier, as if a number were an array type), the mathematical meaning of the number represented by this number is expressed in binary notation to identify a single memory unit. It then interprets the contents of the memory unit and its subsequent units according to the rules of the original type (the length of the type may be more than one byte, and the address type is a type modifier, so it must have the original type). Because the number of variable mappings is actually the address, the number mapped by the variable is the address type. As long A;, assuming a mapping is 3006, when writing a = 3, because a is the variable name, it returns the number 3006 mapped by a, type is a long type of address type. Because it is the address type, the syntax check for the "=" operator succeeds (this is another use of the type-the grammar check, just like the verb name adjective), to perform the calculation of the "=" operator.

It should be noted that C + + did not present the concept of address type modifiers, but I was made out of grammatical completeness, otherwise, more meaningless concepts and rules, such as * (P + 1) = a[2] = 3, will be complicated, so the concept of the number of address types is presented in the "C + + zero-based" series, The aim is to interpret as many grammars as possible with minimal concepts.

Most commonly used type modifiers-pointers and arrays

The pointer is just a type modifier in C + + zero-based (v). When a number is a pointer type, the mathematical meaning of the number represented by this number is expressed in binary notation and returned. The use of numbers mentioned earlier is to convert the number represented by the binary number, whose type only shows how to convert, and the rule that the pointer type represents is the mathematical rule of the binary number, regardless of the type of the original type. Because it is not affected by the original type, the pointer type always has a fixed length (not necessarily for the member pointer) and is four bytes on the 32-bit operating system.

such as long A; long *p = &a;. Suppose a map of the 3006,p is 3010. For *p = 3;,p This operator returns the number 3010 of the address type of a pointer type of type long, that is, the type of the number is decorated by two type modifiers two times, and 3010 is the number of the address type because it is finally modified by the address. And its original type is a long pointer type. So *p returns the number 3006 of the address type of type long, and then executes the calculation of the "=" operator.

Notice here two operators--The content operator "*" and the address Operator "&". In "C + + zero-based (v)" also stressed that they are not in fact, should be called type conversion operations character character pair. That is, the number of the pointer type after the former, the returned number is intact, only its type into the address type, the latter followed by the number of address type, the returned number is intact, only its type into a pointer type. It's a little tricky, but please note: long *p1 = 0 long *p2 = &*p1; If the action of "*" is fetch, then &*P1 will first take the contents of the memory cell address 0, which will cause the memory access violation, but not actually because "*" Only the type is converted rather than content (the content is implemented by the calculation of the number of address types).

As explained earlier, when a number in a pointer type returns a binary number, the participation of the original type is not required, that is, the number 3006 of the type long* and the number 3006 of the type char* returns the same binary number, both of which are 3006 corresponding binary numbers. So why do you want the pointer to be a type modifier with an unused original type? The original type of the pointer type, as you can see earlier, is for the content operator "*". But it's also useful because the addition of the array type modifier allows the pointer to have a so-called computational function, which first looks at the array type.

In the C + + zero-based (v), the modification of an array is described in detail by repeating multiple successive storage of elements of the original type to form a new type. The type of a long a[10];,a is long[10], the length is 10*sizeof (long) = 40 bytes, and the char[7 type corresponds to a length of 7*sizeof (char) = 7 bytes. The type of a number is an array type, because the length of this number can be a byte, can be 10,000 bytes, so this number must be stored in a block of memory, and the number of the array type of the binary return is the memory of its first address. So the constants mentioned above cannot return numbers of an array type because they do not give a piece of memory to hold the number of the array type.

This is a bit confusing, and note that numbers do not have to be stored in memory. For long a[3] = {45, 45, 45}; If the number of a map is 3000, it means that the long[3] rule interprets the number recorded in memory Unit 3000, the length of which is 3*sizeof (long) = 12 bytes, Its value is represented by the decision to use 3000 (the address that records its memory) because the array type is variable in length, and the actual number is 3 values 45. So A; The number 3000 of the address type of the long[3] type is returned first, and then the number of the original type is returned by the number of this address type, which is long[3, and the number is stored in the memory identified by 3000, and the corresponding binary number of 3000 is finally returned.

It is easy to find that the pointer returns an address, and an array is also an address, and when they are of the same type, the latter can be converted implicitly to the former, but not vice versa, because the array also has the same number of elements as the original type of long[2] and long[3. So there are: long a[3]; long *p = A;. There is no problem here, assuming a mapping is 3000, then P's value is 3000. So *p = 3 is the No. 0 element in the number of the array type that holds 3 to 3000. In order to fit into the 1th and 2nd elements, C + + provides a so-called pointer arithmetic function, as follows: * (p + 1) = 4; * (p + 2) = 5;. Here, put 4 in the 1th element, 5 in the 2nd element. to * (p + 1) = 4;,p Returns a long* number of 3000, while P + 1 returns the long* number 3004 and then continues the subsequent calculation. Similarly, p + 2 returns the numeric 3000+2*sizeof (long) =3008 with the type long*. That is, the pointer can only be integer plus minus, for example: char *p1 = 0; p1++; p1 = p1 + 5 * 8-1; short *p2 = 0; P2 + = p2--P1 value of 40,P2 is also 40, because the original type of the P1 is char and the P2 is short. So in order to get the value of the 2nd element of the array, you need * (P + 2); This is obviously not easy to read, so C + + specifically provides a subscript operator "[]", The number preceded by the pointer type, an integer number in square brackets, a number of the pointer type to the address type, and then the value is converted by the pointer operation rule mentioned earlier, and returned. such as long a[4]; long *p = A; Suppose a is mapped to 3000. Then a[2] = 1; equivalent to * (P + 2) = 1;,a[2] The number 3000 (implicit type conversion from long* to Long[4) is preceded by long* F (Long), returns 3008, and the type simply becomes a long type of address type. Because "[]" is simply a simplified and readable version of the pointer operation mentioned above, it can also be a[-1] = 3, which is equivalent to * (p-1) = 3;. Because "[]" before the pointer, it can also be p[-1] = 3;, equivalent to a[-1] = 3;.

Calculates the rule transformation, returns. such as long a[4]; long *p = A; Suppose a is mapped to 3000. Then a[2] = 1; equivalent to * (P + 2) = 1;,a[2] The number 3000 (implicit type conversion from long* to Long[4) is preceded by long* F (Long), returns 3008, and the type simply becomes a long type of address type. Because "[]" is simply a simplified and readable version of the pointer operation mentioned above, it can also be a[-1] = 3, which is equivalent to * (p-1) = 3;. Because "[]" before the pointer, it can also be p[-1] = 3;, equivalent to a[-1] = 3;.

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.