C++

Error: Int Cannot be Dereferenced in C++

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:

#include<iostream>
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:

#include<iostream>
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:

#include<iostream>
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:

#include <iostream>
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:

#include <iostream>
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:

#include <iostream>
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.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.