Programming languages need libraries, functions, syntax, and other things when working with them. Thus, there’s a chance that our codes will contain errors. Programming in C++ has many distinct types of errors. We’ll discuss about the βint cannot be dereferenced in C++β error here. This error occurs when we try to utilize the dereference operator β*β with the integer type data βintβ because βintβ is a primitive data type. We cannot dereference the βintβ in C++ programming. Here, the βint cannot be dereferenced in C++β error and its solution will be explored thoroughly. Some codes are given in this guide in which we will see how this error occurs and how to handle this type of error in C++.
Example 1: Dereferencing Int
In this code, we will try to dereference the βintβ in C++. Now, we simply include the βiostreamβ header file to utilize the defined functions. Underneath this, we utilize the βnamespace stdβ and invoke the βmain()β method here. Then, we declare the βmy_numβ variable with the βintβ data type and assign β99β to this variable.
Now, we declare βmy_valueβ of the βintβ data type and initialize it with β*my_numβ which means that we are trying to dereference the βmy_numβ variable which is of the βintβ data type. The error is generated on this line while compiling because we cannot dereference the βintβ. We want to display the value that we get after dereferencing int, so we place the βcoutβ in which we added the βmy_valueβ variable.
Code 1:
using namespace std;
int main() {
int my_num = 99;
int my_value = *my_num;
cout << "The value we get through dereferencing the int is: " << my_value << endl;
return 0;
}
Output:
Here, we might notice that no value is displayed and an error occurs which says that thereβs an invalid type argument of unary β*β. This means that we cannot dereference the βintβ in C++.
Solution 1: Without Dereferencing
Here, we will get the integer value directly without dereferencing. We import the βiostreamβ header file as some function definitions are present. Then, we type the βnamespace stdβ along with the βusingβ keyword and then call the βmain()β method here. Now, we initialize the βmy_numβ variable of the βintβ data type with the value of β99β.
Then, we initialize another variable with the name βmy_valueβ and assign it with the βmy_numβ variable which contains the integer number. After this, we get this integer value directly without dereferencing by placing βmy_valueβ in the βcoutβ. This gets and displays the integer value.
Code 1:
using namespace std;
int main() {
int my_num = 99;
int my_value = my_num;
cout << "The value we get without dereferencing the int is: " << my_value << endl;
return 0;
}
Output:
The integer value is rendered here which we get without dereferencing in our C++ code. We directly access this integer value and display it.
Solution 2: By Dereferencing the Pointer
The “iostream” header file contains certain function definitions; we imported them. Next, we run the “main()” function here after typing “namespace std” and the “using” keyword. The “my_num” variable of the “int” data type is now initialized with the value of “99”.
After this, we initialize a βmy_ptrβ pointer and assign the address of the βmy_numβ variable to this pointer. Underneath this, we dereference the pointer and assign the value that we get through this to the βmy_valueβ variable as we initialized the βmy_valueβ with the β*my_ptrβ. Now, we place the βcoutβ in which we pass the βmy_valueβ variable to display the integer value which we get through dereferencing the pointer.
Code 2:
using namespace std;
int main() {
int my_num = 99;
int* my_ptr = &my_num;
int my_value = *my_ptr;
cout << "The value of int we get with dereferencing a pointer is: " << my_value << endl;
return 0;
}
Output:
The integer value that we get after dereferencing the pointer in our code is now displayed here. So, this is the correct way for applying dereferencing in C++.
Example 2: Dereferencing Int
Letβs attempt to dereference the βintβ in C++ in this code. To use the functions that are defined in “iostream”, we just need to include the “iostream” header file. This is where we use the “namespace std” and call the “main()” function. Next, we create the “a” variable and give it the “int” data type along with the value of “75”.
Underneath this, we create a pointer named βnewPointerβ and assign the address of the βaβ variable to this pointer with the aid of β&β. Then, we initialize the “num” variable below with “*a” which indicates that we are attempting to dereference the “a” variable which is of the “int” data type. Since we cannot dereference the βintβ, an error is now produced on this line during compilation. After this, we also utilize the βcoutβ in which we add the βnumβ variable.
Code 2:
using namespace std;
int main() {
int a = 75;
int* newPointer = &a;
int num = *a;
cout << "The value the dereferencing the int is: " << num << endl;
return 0;
}
Output:
Here, we can see that there is no value shown and an error message stating an “invalid type parameter of unary”*” appears. This indicates that the βintβ cannot be dereferenced in C++.
Solution 1: Without Dereferencing
We won’t need to dereference toΒ obtain the integer value immediately. We import it since the “iostream” header file contains certain function definitions. The “main()” function is then invoked after the “using” and “namespace std” keywords are inputted. The “a” variable of the “int” data type is now initialized with the value of “75”. Afterward, we insert the “a” into the “cout” to obtain this integer value immediately without dereferencing. This retrieves the integer value and shows it here.
Code 1:
using namespace std;
int main() {
int a = 75;
cout << "The value we get without dereferencing is: " << a << endl;
return 0;
}
Output:
The integer value that is obtained from our C++ code without dereferencing is presented here. This integer value is immediately accessible and is shown here.
Solution 2:
We import the “iostream” header file containing some function definitions. Here, we type the “namespace std” and the “using” keyword before executing the “main()” function. The value of “75” is assigned to the “a” variable.
Next, we declare a pointer called “mypointer” and initialize it with the address of the “a” variable to it. Underneath this, we dereference the pointer and assign the value that we get from this to the βresultβ variable as we initialize the βresultβ with β*mypointerβ. After dereferencing the pointer, we place the “cout” command and pass the “result” variable to show the integer value.
Code 2:
using namespace std;
int main() {
int a = 75;
int* mypointer = &a;
int result = *mypointer;
cout << "The value we get through dereferencing the pointer is: " << result << endl;
return 0;
}
Output:
This is the proper technique to apply dereferencing in C++.Β It renders the integer value that we obtained after dereferencing the pointer instead of dereferencing the βintβ.
Conclusion
We explored that when we try to get the value by dereferencing the βintβ in C++, the βinvalid type parameter of unary “*” having intβ error will occur which means that βint cannot be dereferenced in C++β. We also explained the solution to this error. We used the code in which the errors occurred and the codes in which we provided the solution of that error in this guide.