Home > Backend Development > C++ > C++ error: pointer type and variable type do not match, how to solve it?

C++ error: pointer type and variable type do not match, how to solve it?

WBOY
Release: 2023-08-21 22:13:03
Original
1366 people have browsed it

C is a strongly typed language, and each variable needs to specify its type. When writing programs, pointers are often used. Pointers can point to an address in memory and perform read and write operations on the value at that address. However, when using pointers, there often occurs a mismatch between the pointer type and the variable type, which causes the compiler to report an error.

The problem of mismatch between pointer type and variable type generally occurs in the process of function parameter transfer and assignment. For example, we define an integer variable a, and then define a pointer variable p that points to a floating point type. Then we assign the address of a to the pointer p. The code is as follows:

int a = 10;
float *p;
p = &a;
Copy after login

This code looks like It's simple, but it will cause the compiler to report an error, because the pointer p is a floating point pointer, and a is an integer variable, and the types do not match. At this time, the compiler will prompt us with an error message that the pointer type and variable type do not match.

So how do we solve this problem? There are two ways to solve this.

Method 1: Use pointers of the same type

Method 1 is relatively simple, we only need to define a pointer of the same type. If we need to assign the address of an integer variable to a pointer, then we need to define an integer pointer; if we need to assign the address of a floating-point variable to a pointer, we need to define a floating-point pointer. If we rewrite the above code and define an integer pointer, the code is as follows:

int a = 10;
int *p;
p = &a;
Copy after login

At this time, the compiler will not report an error because the pointer p and the variable a are of the same type.

Method 2: Use type conversion

Method 2 is more flexible. We can change the type of the pointer to the same type as the variable through type conversion. There are three ways of type conversion in C: forced type conversion, static_cast and dynamic_cast. Here we introduce forced type conversion. The code is as follows:

int a = 10;
float *p;
p = (float*)&a;
Copy after login

As you can see, you can perform forced type conversion by adding a small bracket before the pointer type, and then writing the type that needs to be converted into in the bracket. . It should be noted here that forced type conversion may cause program running errors, so when we perform forced type conversion, we need to ensure that the result of the conversion is correct.

Summary:

The problem of mismatch between pointer type and variable type often occurs in C. We can solve this problem by using pointers of the same type or using type conversion. For forced type conversion, you need to use it with caution to ensure that the result of the conversion is correct.

The above is the detailed content of C++ error: pointer type and variable type do not match, how to solve it?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template