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;
}
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
.
the code below moves current2 one node too far from where I want to stop:
typedef struct s_coo
{
int x;
int y;
int z;
void *next;
} t_coo;
typedef struct s_env
{
void *mlx;
void *win;
t_coo **head;
} t_env;
int draw_map_y(t_env *e)
{
t_coo *current;
t_coo *current2;
current = *(e->head);
current2 = (*(e->head))->next;
while (current2->y == 0)
current2 = current2->next;
return (0);
}
So I tried writing in the while loop:
while ((*(*current2))->next->y == 0)
instead of:
while (current2->y == 0)
But I get the error «indirection requires pointer operand». Can anybody explain me and tell me how I could write it the right way? I am pretty new to C. Thank you.
asked Feb 24, 2016 at 8:15
1
while ((*(*current2))->next->y == 0)
is incorrect. As error «indirection requires pointer operand» says, you can apply -> to pointer but you are doing it on (*(*current2)) which is wrong construct (*current2
is object of type struct s_coo
but what should second *
on that structure object do?).
Solution:
while (((t_coo *)current2->next)->y == 0)
What ((t_coo *)current2->next)->y
means is
- take void pointer
current2->next
- and treat it as pointer to
t_coo
(which is typedef onstruct s_coo
) - and then access
y
member on that casted pointer
answered Feb 24, 2016 at 8:25
4pie04pie0
29.1k9 gold badges82 silver badges118 bronze badges
0
The error you are getting «indirection requires pointer operand» is because you are de-referencing the pointer.
Also next pointer is of type void*. You need to type cast it into known pointer type.
This should work,
while(((t_coo*)(current2->next))->y == 0)
answered Feb 24, 2016 at 8:26
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.