Indirection requires pointer operand ошибка

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

yuniFlaminjo's user avatar

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

MARS's user avatar

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

.

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

zakk8889's user avatar

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

  1. take void pointer current2->next
  2. and treat it as pointer to t_coo (which is typedef on struct s_coo)
  3. and then access y member on that casted pointer

answered Feb 24, 2016 at 8:25

4pie0's user avatar

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

Rohit Magdum's user avatar

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <initializer_list>
#include <iostream>

template <typename T>
class List {
  struct Node;  
  class It;
 public:
  List() {
    std::initializer_list<T> empty;
  }

  List(std::initializer_list<T> ele) {
    this->operator=(ele);  
  }

std::size_t Size() const {
    int count = 0;
    Node* current = start;
    while (current != NULL) {
      count++;
      current = current->next;
    }
    return count;
  }

  void Insert(T ele) {
    Node* node = new Node{ele};
    if (this->size == 0) {
      this->start = this->fin = node;
    } else {
      this->fin->next = node;
      node->prev = this->fin;
      this->fin = node;
    }
    ++this->size;
  }

  void RemoveFront() {
    Node* front = this->start;
    this->start = front->next;
    delete front; 
  }

  T& Front() const {
     if (Size() <= 0) {
       return Front();
     }
    Node* Front = start;
    return Front->data;
  }

  List& operator=(std::initializer_list<T> ele) {
    this->size = 0;
    for (auto&& val : ele) this->Insert(val);
    return *this;
  }

  friend std::ostream& operator<<(std::ostream& out, const List& list) {
    for (auto val = list.start; val; val = val->next) {
      out << val->data;
      if (val->next) out << ",";
    }
    return out;
  }

  template <class It>
  List(It begin, It end) {
    for (; begin != end; ++begin) this->Insert(*begin);
  }

  It begin() {
    return It(start);
  }

  It end() {
    return It(fin);
  }

private:
  struct Node {
    T data;
    Node* next = nullptr;
    Node* prev = nullptr;
  };

  class It {
    friend class List;
    explicit It (List::Node *ptr) : cur(ptr) {}
     typename List<T>::Node* cur;
    List<T>* theList;
    public:
    It& operator++() {
      this->cur;
      return *this;
    }
    It operator++(int) {
      It temp(*this);
      this->operator++();
      return temp;
    }
     bool operator!=(It val) const { 
       return !(this->operator==(val));
     }
     bool operator==(It val) const { 
       return !(this->operator!=(val));
     }
  };

  Node* start = nullptr;
  Node* fin = nullptr;
  std::size_t size = 0;
};

int main() {
List<char> test = {'H', 'e', 'l', 'l', 'o'};
std::cout << test << std::endl;
List<char> first (test.begin(), test.end());
}

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.

1
2
3
It& begin() {
    return It(start);
}

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:

1
2
3
It begin() {
    return It(start);
}

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:

1
2
3
4
5
T const *operator->() const { return &(**this); }
T *operator->() { return &(**this); }
    
T& operator*() const { return this->cur->data; }
T const &operator*() { return this->cur->data; }

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:

1
2
3
4
5
6
bool operator!=(It val) const { 
  return !(this->operator==(val));
}
bool operator==(It val) const {
  return !(this->operator!=(val));
}

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:

1
2
3
bool operator==(It val) const { 
  return this->cur == val->cur; 
}

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.

Понравилась статья? Поделить с друзьями:

Не пропустите эти материалы по теме:

  • Яндекс еда ошибка привязки карты
  • Infiniti g25 ошибка c1a26
  • Indexerror string index out of range ошибка
  • Infiniti fx35 сброс ошибок
  • Indexerror list index out of range python ошибка

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии