Problems with type conversion in C + +

Source: Internet
Author: User

1. From the simplest of words:

First, the conversion between an int variable and a char variable is divided into implicit conversion and display transformation.

For example: int i; char c; i = C;

The implicit conversion, the compiler implicitly converts a char variable into an int type.

For example: int i; char c; i = (int) c;

The conversion is displayed very much, indicating the type to be converted.

Whether a display conversion or an implicit conversion is risky, there is a risk that a large memory model (such as int) can be converted to a small memory model (such as Char) with truncation.

However, the transition from a small memory model to a large memory model is a high-level complement (a positive complement of 0, a negative complement of 1).

2. Type conversion of pointers

For example: a = 1;int *p = &a;float *PF = (float*) p;

The values of P and pf are all &a, but *p is interpreted by the value in the &a address as an int variable, and *p1 is interpreted as a value in the &a address according to the float variable.

Example: int a;float *p;p = &a;

The compiler will make an error and the pointer type does not match. Note that a certain type of conversion can be made between pointers, but pointers have a specific type, and you cannot assign a pointer of one type to a pointer of another type, unless you add a cast.

For example: int a;float *p;p = (float*) &a;

This is possible.

Problems with type conversion in C + +

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.