Do not hurt the pointer (7)--pointer type conversion

Source: Internet
Author: User

When we initialize a pointer or assign a value to a pointer, the left side of the assignment number is a pointer, and the right side of the assignment number is a pointer expression. In the example above, in most cases, the type of the pointer is the same as the type of the pointer expression, and the pointer points to the same type as the pointer expression.

Example 15:

float F = 12.3;
float *fptr = &f;
int *p;

In the example above, what if we want the pointer p to point to the real f? Is it using the following statement?

p = &f;

Wrong. Because the type of pointer p is int *, the type it points to is int. The result of the expression &f is a pointer, the type of the pointer is float *, and the type it points to is float. There is no inconsistency between the two, the direct assignment method is not possible. At least on my msvc++6.0, the assignment statement for the pointer requires the same type on both sides of the assignment, and the same type, and the other compiler I haven't tried, you can try. In order to achieve our goal, a "forced type conversion" is required:

p = (int*) &f;

If there is a pointer p, we need to change its type and the type of the pointer to Tyep *type, then the syntax format is: (TYPE *) p;

The result of forcing the type conversion is a new pointer that is of type *, which is a type that points to the address that the original pointer points to. All the properties of the original pointer p have not been modified. Remember

A function if you use a pointer as a parameter, you must ensure that the type is consistent during the combination of the arguments and the parameters of the function call statement, otherwise you need to cast

Example 16:

voidFunChar*);intA = the, B;fun (Char*) &a);voidFunChar*s) {    CharC; C=* (s+3); * (s+3) =* (s+0); * (s+0)=C; C=* (s+2); * (s+2) =* (s+1); * (s+1)=C;}

Note that this is a 32-bit program, so the int type takes up four bytes, and the char type takes up one byte. The function of fun is to reverse the order of four bytes of an integer. Have you noticed? In a function call statement, the result of an argument &a is a pointer, which is of type int *, and it points to an int. Parameter the type of the pointer is char *, which is the type of char. Thus, during the combination of the arguments and the parameters, we have to do a conversion from the int * type to the char * type. With this example, we can imagine the compiler's process of converting: The compiler constructs a temporary pointer char *temp, then executes the temp= (char *) &a, and finally passes the value of temp to S. So the final result is that the type of S is char *, which is the type char, and the address it points to is the first address of a.

We already know that the pointer value is the address pointed to by the pointer, in a 32-bit program, the value of the pointer is actually a 32-bit integer. Would it be possible to assign an integer to the pointer directly as a value of the pointer? Just like the following statement:

unsigned int A;
TYPE *ptr; Type is Int,char or struct type, and so on.
A = 20345686;
ptr = 20345686; Our goal is to make the pointer ptr point to address 20345686
ptr = A; Our goal is to make the pointer ptr point to address 20345686

Compile it. It turns out that the last two statements are all wrong. So we're not going to be able to achieve this? No, there are ways:

unsigned int A;
TYPE *ptr; Type is Int,char or struct type, and so on.
A = N//n must represent a valid address;
ptr = (type*) A;//hehe, that's it.

Strictly speaking, the (type *) is not the same as the (type *) in the pointer type conversion. Here (type*) means to treat the value of unsigned integer a as an address. It is emphasized that the value of a must represent a valid address, otherwise an illegal operation error occurs when you use PTR.

Think about the reverse, take the pointer to the address that the pointer value as an integer to take out. It's perfectly possible. The following example shows that the value of a pointer is taken as an integer, and then the integer is assigned to a pointer as an address:

Example 17:

int 123 , B; int *ptr = &A; Char *= (int// The value of the pointer ptr is taken as an integer. str = (char// assigns the value of this integer as an address to the pointer str. 

Now we know that the value of the pointer can be taken as an integer, or an integer value can be assigned to a pointer as an address.

Do not hurt the pointer (7)--pointer type conversion

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.