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.