Type conversion of pointer variables and pointer Variables

Source: Internet
Author: User
Emphasize one point before you forget:

We usually talk about pointer variables instead of pointers.
Pointer = the address of the variable, and pointer variable is the variable that stores the address of the variable.
So the first concept of a pointer variable should be: it is a variable and then understands its behavior and implementation.
Example:
Int A = 10;
Void * B;
B = &;

Printf ("% P/N", B );
// * B = 20;
Int * c = (int *) B;
* C =;
Printf ("% P", C );

The above small exerciseCodeDescribes the basic operations for NULL pointer variables.

Null Pointer variables are also pointer variables, so you can assign B = & A; to them, and when printing the address (printf ("% P/N", B);), it can also print the address of a correctly.

[// * B = 20 ;]
This code is definitely incorrect, because B is a null pointer, that is, the compiler cannot determine the size of the memory space referred to by the pointer variable, the compiler identifies the type of the pointer variable (if int * a, the type of A is int) to determine the size of the memory space referred to by the pointer variable, in this way, * B is parsed into a direct address of 4 bytes of the int type, and the value assigned to it is OK.

[Int * c = (int *) B ;]
This Code shows that you can convert the pointer type and convert the void * type to int * type. first look at the following code:
Int *;
Char * B;
Char c = 'a ';
B = & C;
A = (int *) B;
Printf ("% P/N", );
Printf ("% P", B );
This Code shows that converting from char * to int * is okay, so let's look at the next section:
Int * E;
Double * F;
Double H = 2.00f;
F = & H;
E = (int *) F;
Printf ("% P/T", e );
Printf ("% d/N", * E );
Printf ("% P/T", F );
Printf ("% F/N", * F); // do not forget to add *
Compile this code and pass it safely. The output is:
0012ff70 0
0012ff70 2.000000
It is okay to see f pointer variables, but the output value of E pointer variables does not seem to be what you want, or you think this sentence [printf ("% d/N ", * E);] It does not seem very practical. We can change it to [printf ("% F/N", * E);] give a try and output the result:
0012ff70 0.000000
0012ff70 2.000000
According to my understanding: Obviously, the expression of integers and floating-point numbers in the memory is different. Although the pointer is a variable consisting of four bytes, it holds an address, however, the compiler determines the number of bytes pointing to the address based on its type (for example, Int Is 4 bytes and char is one byte ), and the meaning of these bytes (obviously, the int of the same four bytes is different from that of the float machine.

It seems a little long to lead such a long string from a null pointer, but it is just to remind everyone that pointers can be converted to each other. But please refer to the conversion rules between basic types, I am aware that nothing can be switched, so as to reduce the number of bugs.

of course, the safer method is based on the Program context, that is, when you know what you are doing (for example: the program only wants to obtain a pointer pointing to a memory unit to determine the necessary usage.) It is advisable to convert the NULL pointer to implement some specific functions, however, if you do not consider the other items, you can leave them alone.

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.