Pointer topic 2

zhaozj2021-02-16  126

From http://community.9cbs.net/expert/topic/3172/3172383.xml?temp=.1944544

In fact, there is no so-called "pointer type", and only the type of object referred to as the pointer. In other words, the pointer only saves one container of the object address, and the type of object added is only a compile period information, which is used to prompt the compiler that is taken out from this address to take out how many bytes are delivered. So you can't see the type of object referred to by the pointer and the type of the pointer itself, but you should take them as a thing from the beginning. It can be said that there are many types of types in the type system, there should be a pointer (similarly, how many references can be used, how many arrays, how many kinds of ... :). Oh, the words, the next one, explain the type of pointer, by the way, by means of a place where the needle refers to use.

(PS: Just now said "TCPL" regards pointer as a "derived type", which is worthwhile.)

1, the simplest pointer: pointing to a single object (why do I say "object" instead of "variables"? Because the object is the Dongdong, the variable is only the object of the object). Examples are as follows:

INT * PI; // Declaration Defines a pointer to the int type object

The above statement is just simply declared defines a pointer, and the name of the pointer is introduced in the symbol table, and the type information is saved. But such a way of writing may be alarm, because the C compiler will throw a warning for the pointer that is not initialized (the programs that caught careless, the local variable directly defined is random, but Object references to a pointer with a random address ... Bombing!). So we best write this:

INT A, * PI = & a; or int * pi = null; // Domestic C related exams favorite here

Remember a little, define a pointer to initialize it, it's right. The object referred to is also very simple, * Pi is it. Concept can use * pi as a use, but pay attention to the priority of the operator (such as * p and * (p ) and * p ... headache? :).

2, more complex pointers: points to the pointer to the composite object. Composite objects may be data aggregation (such as arrays, structures, enumerations), may also be a type of aggregation (such as a consortium), in other words, the composite object is "user-defined type", which is different. But regardless of how they change, there will always be a corresponding pointer to their instance objects (in the book saying that the pointer is other type of derived type, the more I think about it.). Example 1:

Struct human {char name [24]; int Age;} h; struct human * ph = & h;

To reference a member of the structure through the pointer, you can use the-> operator, such as ph-> name [0] or ph-> agn. Similarly, * pH is conceptual with H equivalence, which can be used in this way, sometimes plus a parental brackets, (* pH) .age).

Example 2: Union msg_entry {INT (* Button_Click); Void (* idle);} me; union msg_entry * pm = & me; the member to reference the consortium through the pointer, can also be used - > Operators, such as PM-> Button_Click, or PM-> iDLE. * PM is also equivalent to ME. However, note that the address of both PM-> Button_Click and PM-> iDLE is actually the same. This is the basic difference between the consortium and the structure.

Example 3: Typedef Int Array [10]; Array A; Array * PA = & A;

Here, the type of object (i.e., an object) referred to by PA may be confusing. However, after careful analysis, you can know that the type of A is an array of 10 integer elements, while PA is a pointer to this object. That is, the type of PA should be int (*) [10]. Please pay attention to (*) and [10], the former is defined as "point to array", which limits "The size of the array". If you have the following code: INT B [5]; PA = & B; // The compiler should alarm, because int [5] type cannot be compatible with int (*) [10]

Hey ... I understand? That is, the array size is also part of the defined decoration.

There are also a very easier confusing type: int * p [10]; int (* pp) [10]; int * (* ppp) [10];

Among them, P is an array that has 10 INT * types. And PP is a pointer, pointing to an INT [10] type of array object. PPP is also a pointer, pointing to an array object of the INT * [10] type. So you can write: PPP = & P; // can not write this: PP = & p; // The type of the two cannot be compatible

(PS: Related to the relationship between arrays and pointers, you can refer to I wrote another small topic, URL is http://community.9cbs.net/expert/topic/3142/3142075.xml?temp =.2214624. And array The relationship with the pointer can also be expanded, this is a later, pressing the table)

转载请注明原文地址:https://www.9cbs.com/read-13984.html

New Post(0)