I have the following functions, an initializeHeap function which takes in no arguments and outputs a Heap struct which I have defined. And an insertNode function which takes in a pointer to a heap struct and the number to be added to the heap. I call them in the main function as such:
h = initializeHeap();
Heap *p = *h;
insertNode(p,5);
insertNode(p,7);
insertNode(p,3);
insertNode(p,2);
I get this error when trying to do this:
error: indirection requires pointer operand
Any ideas? I can post more code if needed.
The struct Heap and function initializeHeap() are as follows:
typedef struct node{
int data;
}Node;
typedef struct heap{
int size;
Node *dataArray;
}Heap;
Heap initializeHeap(){
Heap heap;
heap.size = 0;
return heap;
}
The Indirection requires pointer operand error message is shown on the screen when the programmer has created a pointer to a variable by using *my_var instead of &my_var.
This article will provide detailed knowledge of this error and ways to solve it. First, let’s start with the reasons that cause this issue and find the best solutions.
Contents
- Why Does the Indirection Requires Pointer Operand Error Occur?
- – Error in Syntax
- – Cannot Take the Address of an Rvalue of Type ‘Byte’
- – Using the Wrong Function
- – Compiling Error
- – Wrong Usage of the Buffer Function
- – Dereferencing the Wrong Array Value
- How To Resolve the Indirection Requires Pointer Operand Error?
- – Correct the Syntax Errors
- – Use Vector of Pointers C++ To Resolve the Error
- – Solving (Cannot Take the Address of an Rvalue of Type ‘Byte’) Error
- – Pass Pointer to Function C++: Compilation
- Conclusion
- Reference
The Indirection requires pointer operand error message occurs while programming because the compiler interpreted the indirect pointer operand. Some of the other reasons include syntax errors or compiling errors along with cannot take the address of an rvalue of type ‘byte’.
Other common reasons behind this situation involve:
- Using the wrong function
- Compiling error
- Wrong usage of the Buffer function
- Dereferencing the wrong array value
– Error in Syntax
Syntax errors are common while typing large programs. Syntax errors can include writing the wrong functions or using operations that create an undefined behavior. If the programmer is taking the address of a temporary object of type (any), and it is not defined, then during execution, an error will occur.
Moreover, if the programmer wants to create a pointer to their variable by using *my_var instead of &my_var, a syntax error will occur along with the pointer operand error message.
– Cannot Take the Address of an Rvalue of Type ‘Byte’
When the program cannot take the address of an rvalue of type ‘byte’, it shows a pointer error. It occurs when the conditional expression does not produce an lvalue. Following is an example for better understanding:
For Example:
a := ‘hello beautiful world’
for a := 1; a < a.len; a++ {
// bite := b[a]
str := tos3(b[a])
println(str)
}
}
– Using the Wrong Function
When a wrong function is used, just like a syntax error, it also shows an operand error in the program. In the below example, the atoi function returns an int, not int *.
Thus, there is no need to dereference the return value again. The compiler will show an error message if the programmer tries to dereference int. Here’s an example:
For example:
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <curses.h>
int main(int argc,char *argv[])
{
unsigned long int virtualAddress,pageNumber,offset;
if(argc<2){
printf(” NO ARGUMENT IS PASSED”);
return 0;
}
virtualAddress = *atoi(argv[1]);
//PRINT THE VIRTUAL ADDRESS
printf(“The Address of %lu contains:”,virtualAddress);
//CALCULATE THE PAGENUMBER
pageNumber = virtualAddress/4096;
//PRINT THE PAGE NUMBER
printf(“n The Page Number = %lu”,pageNumber);
//FIND THE OFFSET
offset = virtualAddress%4096;
//PRINTS THE OFFSET
printf(“n The Offset = %lu”,offset);
getch();
return 0;
}
The error in here is “virtualAddress = *atoi(argv[2]);”.
– Compiling Error
When the compiler interprets the program as an indirect operation of a pointer, it means the compiler is an incompatible pointer to integer conversion, and so, the error occurs during execution.
In other words, if the compiler and pointer are incompatible, a pointer error will occur. Let’s explain this using an example.
Program Example:
long long *FindLength = *card;
long long *FindFirstTwoDigits = *card;
Explanation:
In this example, the programmer has used owned operators of a pointer to a variable of integer type. Thus, the compiler interprets this as an indirect operation of a pointer.
Moreover, the function (long long * FindLength) declares a pointer to an integer of type long long, but since a compiling error occurred, the program will instead show an error message.
– Wrong Usage of the Buffer Function
Understanding the buffer function is important because the wrong usage of it can cause errors such as pointer issues in the program. Some programmers confuse between buffer and &buffer[0].
The programmer should use the buffer as an argument instead of &buffer[0]. Passing an array has the same procedure as passing a pointer to its first element or passing pointers to functions.
In the example below, the programmer has used the wrong buffer function in their array. The &buffer[0] causes a pointer operand error message to occur.
For example:
bool startJPGchecker(BYTE *buffer)
{
if (*buffer[0] == 0xff && *buffer[1] == 0xd8 && *buffer[2] == 0xff && *buffer[3] >= 0xe0 && *buffer[3] <= 0xef)
{
return true;
}
else
{
return false;
}
}
– Dereferencing the Wrong Array Value
When the programmer uses arr[n] within the function, it gives them the nth value in that array instead of giving a pointer. Therefore, if the user is trying to dereference that value (*arr[n]), it will not work unless they use an array of pointers with it.
How To Resolve the Indirection Requires Pointer Operand Error?
To resolve the Indirection requires pointer operand error message, the programmer has to pass their pointers from the functions or use correct, defined, and direct pointers and commands. Furthermore, this error can be fixed by solving the cannot take the address of an Rvalue of a type byte error.
Let’s see all the possible reasons that can solve this error. They are explained below in detail.
– Correct the Syntax Errors
Using the correct functions will eliminate syntax errors and get rid of error messages related to pointers. There are various software that help in detecting these errors and help the user to correct them.
Let’s take an example: the programmer is dereferencing a long program and needs it in an unsigned long int. Then instead of using “atoi” in “virtualAddress = *atoi(argv[1]);”, they should use the strtoul function. Here’s an example:
Program:
virtualAddress = strtoul(argv[2], &p,11);
– Use Vector of Pointers C++ To Resolve the Error
When the programmer uses a vector of pointers c++ to resolve the error, they have to be careful with the pointers and operands. With the help of this function, the programmer can avoid pointer errors. Vector is used as an alternative to some pointers. Here’s an example;
For example:
#include <vector>
using namespace std;
int main()
{
char ch1 = ‘A’, ch2 = ‘B’, ch3 = ‘C’, ch4 = ‘D’, ch5 = ‘E’, ch6 = ‘F’;
vector vtr = {&ch3, &ch4, &ch3, &ch6, &ch7, &ch9};
for (int a=0; a<vtr.size(); a++)
cout << *vtr[a] << ‘ ‘;
cout << endl;
for (vector::iterator as = vtr.begin(); as != vtr.end(); as++)
cout << **as << ‘ ‘;
cout << endl;
return 0;
}
The output:
As you can see, the same list of alphabets is displayed twice. To elaborate, the first statement in the main() function creates six characters with their identifiers or pointers. Whereas the second statement represents these characters with their memory addresses, resulting in a vector of pointers to chars.
– Solving (Cannot Take the Address of an Rvalue of Type ‘Byte’) Error
When the rvalue of byte type is not addressed, the (cannot take the address of an rvalue of type ‘byte’) error message occurs. Due to this error, a pointer error also arises. Therefore, the programmer must use the correct commands to remove this error.
Let’s see the correct commands from the example above.
Program
a := ‘hello beautiful world’
for i := 0; i < a.len; i++ {
unsafe{
str := tos(a.str[i],1)
println(str)
}
}
}
– Pass Pointer to Function C++: Compilation
As mentioned above, the user cannot apply the owned operators of a pointer to a variable of integer type. If they do that, the compiler will show an error because it will interpret the program as an indirect pointer operation.
Therefore, to remove this error, the programmer has to correct the syntax error and pass pointer to function C++. This means the pointer has to pass from the function for the compiler to interpret it correctly. However, this is suitable if the user is using C++ language. Let’s take an example.
Wrong program:
long long *FindLength = *card;
long long *FindFirstTwoDigits = *card;
Correct program:
long long * FindLength = &card;
long long *FindLength = *card;
long long *FindFirstTwoDigits = *card;
Conclusion
After reading this article completely, the reader will have maximum knowledge about this error and they will be able to resolve it using the different methods shown in this article. Some important points to keep in mind are:
- The pointer should define the operator. Otherwise, the error will occur.
- No usage of pointers can cause the same error to occur.
- While creating the pointers that will support the variables in the program, make sure to use the right functions.
The reader can now proceed to resolve similar programming errors by the guidance provided in this article. Thank you for reading!
Reference
- https://cs50.stackexchange.com/questions/30016/pset1-error-indirection-requires-pointer-operand
- https://stackoverflow.com/questions/38237355/error-indirection-requires-pointer-operand-int-invalid
- https://www.reddit.com/r/cs50/comments/4c7gn4/passing_pointers_to_functions_error_indirection/
- https://github.com/vlang/v/issues/3336
- https://linuxhint.com/cpp-vector-pointers-examples/#2
- Author
- Recent Posts
Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL. Meet The Team
If you’re a developer, you may have come across the error message «Indirection requires pointer operand» when trying to compile your code. This error message is usually caused by an invalid ‘int’ type, which means that the compiler is expecting a pointer instead of an integer.
In this guide, we’ll provide tips and solutions for fixing the ‘Indirection Requires Pointer Operand’ error in your code.
Understanding the ‘Indirection Requires Pointer Operand’ Error
Before we dive into the solutions, let’s take a closer look at what the error message means.
In C and C++, the * operator is used for indirection, which means it accesses the value stored at the address pointed to by a pointer variable. For example, if you have a pointer variable ptr that points to an integer variable num, you can access the value of num using the * operator like this:
int num = 10;
int* ptr = #
int value = *ptr; // value is now 10
However, if you try to use the * operator on a non-pointer variable like an integer, you’ll get the ‘Indirection Requires Pointer Operand’ error. For example:
int num = 10;
int value = *num; // error: indirection requires pointer operand ('int' invalid)
This error message means that the compiler is expecting a pointer instead of an integer.
Solutions for the ‘Indirection Requires Pointer Operand’ Error
There are several ways to fix the ‘Indirection Requires Pointer Operand’ error, depending on the root cause of the problem.
Solution 1: Use a Pointer Variable
The simplest solution is to use a pointer variable instead of a non-pointer variable. For example:
int num = 10;
int* ptr = #
int value = *ptr; // value is now 10
Solution 2: Cast the Integer to a Pointer
If you need to use an integer variable as a pointer, you can cast it to a pointer type like this:
int num = 10;
int* ptr = (int*)num;
int value = *ptr; // value is now 10
Note that this solution is not recommended, as it can lead to undefined behavior if the integer value is not a valid memory address.
Solution 3: Use the Address-of Operator
You can also use the address-of operator (&) to get the memory address of an integer variable and assign it to a pointer variable like this:
int num = 10;
int* ptr = #
int value = *ptr; // value is now 10
This solution is similar to Solution 1 but may be useful in certain situations where you don’t have a pointer variable available.
FAQ
Q1: What causes the ‘Indirection Requires Pointer Operand’ error?
A: The error is caused by trying to use the * operator on a non-pointer variable like an integer.
Q2: Can I use a cast to fix the error?
A: Yes, you can cast the integer to a pointer type, but this is not recommended as it can lead to undefined behavior.
Q3: How can I avoid the error in the first place?
A: Make sure to use pointer variables when using the * operator for indirection.
Q4: Is the error specific to C and C++?
A: Yes, the error message is specific to C and C++.
Q5: Can I use the address-of operator to fix the error?
A: Yes, you can use the address-of operator to get the memory address of an integer variable and assign it to a pointer variable.
- Pointer Basics in C and C++
- Common C++ Compiler Errors and How to Fix Them
- Casting in C and C++
- The Address-of Operator in C and C++
We hope this guide has been helpful in fixing the ‘Indirection Requires Pointer Operand’ error in your code. If you have any questions or suggestions on how to improve the guide, please let us know in the comments below.
Here is a snippet of my code:
long long card = get_long_long();
long long *FindLength = *card;
long long *FindFirstTwoDigits = *card;
And here are the errors:
credit1.c:8:29: error: indirection requires pointer operand ('long long'
invalid)
long long *FindLength = *card;
^~~~~
credit1.c:10:37: error: indirection requires pointer operand ('long long'
invalid)
long long *FindFirstTwoDigits = *card;
Could somebody help me with this?
Thanks in advance.
asked Aug 28, 2018 at 19:22
Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL. Meet The Team
If you’re a developer, you may have come across the error message «Indirection requires pointer operand» when trying to compile your code. This error message is usually caused by an invalid ‘int’ type, which means that the compiler is expecting a pointer instead of an integer.
In this guide, we’ll provide tips and solutions for fixing the ‘Indirection Requires Pointer Operand’ error in your code.
Understanding the ‘Indirection Requires Pointer Operand’ Error
Before we dive into the solutions, let’s take a closer look at what the error message means.
In C and C++, the * operator is used for indirection, which means it accesses the value stored at the address pointed to by a pointer variable. For example, if you have a pointer variable ptr that points to an integer variable num, you can access the value of num using the * operator like this:
int num = 10;
int* ptr = #
int value = *ptr; // value is now 10
However, if you try to use the * operator on a non-pointer variable like an integer, you’ll get the ‘Indirection Requires Pointer Operand’ error. For example:
int num = 10;
int value = *num; // error: indirection requires pointer operand ('int' invalid)
This error message means that the compiler is expecting a pointer instead of an integer.
Solutions for the ‘Indirection Requires Pointer Operand’ Error
There are several ways to fix the ‘Indirection Requires Pointer Operand’ error, depending on the root cause of the problem.
Solution 1: Use a Pointer Variable
The simplest solution is to use a pointer variable instead of a non-pointer variable. For example:
int num = 10;
int* ptr = #
int value = *ptr; // value is now 10
Solution 2: Cast the Integer to a Pointer
If you need to use an integer variable as a pointer, you can cast it to a pointer type like this:
int num = 10;
int* ptr = (int*)num;
int value = *ptr; // value is now 10
Note that this solution is not recommended, as it can lead to undefined behavior if the integer value is not a valid memory address.
Solution 3: Use the Address-of Operator
You can also use the address-of operator (&) to get the memory address of an integer variable and assign it to a pointer variable like this:
int num = 10;
int* ptr = #
int value = *ptr; // value is now 10
This solution is similar to Solution 1 but may be useful in certain situations where you don’t have a pointer variable available.
FAQ
Q1: What causes the ‘Indirection Requires Pointer Operand’ error?
A: The error is caused by trying to use the * operator on a non-pointer variable like an integer.
Q2: Can I use a cast to fix the error?
A: Yes, you can cast the integer to a pointer type, but this is not recommended as it can lead to undefined behavior.
Q3: How can I avoid the error in the first place?
A: Make sure to use pointer variables when using the * operator for indirection.
Q4: Is the error specific to C and C++?
A: Yes, the error message is specific to C and C++.
Q5: Can I use the address-of operator to fix the error?
A: Yes, you can use the address-of operator to get the memory address of an integer variable and assign it to a pointer variable.
- Pointer Basics in C and C++
- Common C++ Compiler Errors and How to Fix Them
- Casting in C and C++
- The Address-of Operator in C and C++
We hope this guide has been helpful in fixing the ‘Indirection Requires Pointer Operand’ error in your code. If you have any questions or suggestions on how to improve the guide, please let us know in the comments below.
Here is a snippet of my code:
long long card = get_long_long();
long long *FindLength = *card;
long long *FindFirstTwoDigits = *card;
And here are the errors:
credit1.c:8:29: error: indirection requires pointer operand ('long long'
invalid)
long long *FindLength = *card;
^~~~~
credit1.c:10:37: error: indirection requires pointer operand ('long long'
invalid)
long long *FindFirstTwoDigits = *card;
Could somebody help me with this?
Thanks in advance.
asked Aug 28, 2018 at 19:22
What is supposed to be * card? We can not apply own operators of a pointer to a variable of integer type in this case. It seems that the compiler is interpreting this as an indirect operation of a pointer.
Well we have the statement:
long long * FindLength
this declares a pointer to an integer of type long long, a pointer can only contain a memory address, if what you want to do is assign the memory address of the card variable to FindLength you should do the following
long long * FindLength = &card;
I hope this answers your question, keep in mind that it is not very clear and I do not know exactly your intentions.
answered Aug 28, 2018 at 20:03
MARSMARS
5,2013 gold badges13 silver badges23 bronze badges
1
You must log in to answer this question.
Not the answer you’re looking for? Browse other questions tagged
.
Not the answer you’re looking for? Browse other questions tagged
.
error pointer operand
I get this error when I try compiling my code is there a way I can fix this.
testings.cpp:69:48: error: indirection requires pointer operand
(‘List<char>::It’ invalid)
for (; begin != end; ++begin) this->Insert(*begin);
^~~~~~
testings.cpp:114:12: note: in instantiation of function template specialization
‘List<char>::List<List<char>::It>’ requested here
List<char> first (test.begin(), test.end());
^
testings.cpp:94:13: warning: expression result unused [-Wunused-value]
this->cur;
~~~~ ^~~
testings.cpp:69:26: note: in instantiation of member function
‘List<char>::It::operator++’ requested here
for (; begin != end; ++begin) this->Insert(*begin);
^
testings.cpp:114:12: note: in instantiation of function template specialization
‘List<char>::List<List<char>::It>’ requested here
List<char> first (test.begin(), test.end());
^
1 warning and 1 error generated.
|
|
Last edited on
Don’t return a reference, just return It.
You haven’t defined operator!= for your iterators. (Although you might find that setting your end iterators internal pointer to «fin» is not going to work too well.)
Your Size method actually counts the nodes in the list, even though you have a size member that you could return (as long as you update it properly; for instance, RemoveFront should subtract 1 from it.).
Your default ctor has a pointless initializer_list in it.
|
|
In this function, the expression
It(start)
is a temporary object. The language refuses to take a (non-const lvalue) reference to it, because the referent would be destroyed immediately at the semicolon of the return statement.
You don’t want to return a reference to a temporary value at all: just return by value:
|
|
Same advice for
end()
.
Then, your problem is that you never implemented operator overloads for
List::It
.
You also have never provided a suitable constructor for
List::Node
.
Last edited on
So I edited the code and added what you specified and the new operators but now I get this new error and also how could I rewrite my operator++() so it moves through each iterator?
Which new error?
Sorry @tpb, I didn’t see your post until now.
I redid the post n edited the code and posted the new error now I get just one error also I’m wondering if there is a way to change the operator++() so it moves through each iterator or the the next one.
You need to add a dereference operator (and indirect member access) for
It
, these will do:
|
|
Although this is a bit confusing because
List::It
is overly complicated.
These functions never return; they call each other for ever. You have to actually write at least one of them:
|
|
It is generally easier to understand conditions that aren’t inverted, so we’ll prefer to write == and leave != like it is:
This one will do:
|
|
This is enough to get the code to compile: see
http://coliru.stacked-crooked.com/a/dc3c05c3e8d61188
And some advice:
The key to fixing an incorrect program is to attack problems one at a time. Because new code is always broken, it’s absolute madness to continually pile new untested code atop broken components in the hope of fixing everything later.
Test your code continually, piece-by-piece as you write it; fix visible errors as you make them.
Last edited on
thanks I got it too work also
I redid the post n edited the code
Please DON’T do that. It makes the thread impossible to understand for anyone coming to it fresh, and makes it useless as a learning resource for others.
Topic archived. No new replies allowed.