0

In the below code there is a node pointer naya in the mn function. In the if condition first time the naya pointer is pointing to the null and we are trying to access its data. If we try to do the same code without the brackets around the naya pointer, then it gives error such as:

prog.cpp: In function ‘void mn(Node*, Node**)’:
prog.cpp:66:50: error: request for member ‘data’ in ‘* naya’, which is of pointer type ‘Node*’ (maybe you meant to use ‘->’ ?)
     if(*naya == NULL || temp -> data <= *naya -> data)
                                             ^*

But when we are using the brackets its' working fine. Why?

Below is the whole code:

#include <bits/stdc++.h>
using namespace std;
class Node {
 public:
  int data;
  Node* next = NULL;
  Node(int x) {
    data = x;
    next = NULL;
  }
};

void mn(Node* temp, Node** naya) {
  Node* current;

  if (*naya == NULL || temp->data <= (*naya)->data) {
    temp->next = *naya;
    *naya = temp;
  } else {
    current = *naya;
    while (current->next != NULL && (current->next->data < temp->data)) {
      current = current->next;
    }
    temp->next = current->next;
    current->next = temp;
  }
}
Node* isort(Node* head) {
  Node* temp = head;
  Node* naya = NULL;
  while (temp != NULL) {
    Node* nex1 = temp->next;
    mn(temp, &naya);
    temp = nex1;
  }
  return naya;
}
void printll(Node* head) {
  Node* temp = head;
  while (temp != NULL) {
    cout << temp->data;
    temp = temp->next;
  }
}
int main() {
  Node *head = NULL, *temp = NULL;
  int a;
  cin >> a;
  for (int i = 0; i < a; i++) {
    int x;
    cin >> x;
    Node* newnode = new Node(x);
    if (head == NULL) {
      head = newnode;
      temp = head;
    } else {
      temp->next = newnode;
      temp = temp->next;
    }
  }
  head = isort(head);
  printll(head);
}
David G
  • 94,763
  • 41
  • 167
  • 253
  • Tip: Express things like `current->next` without the spaces. Spaces make it significantly harder to parse when reading. – tadman Jun 02 '20 at 22:04
  • 7
    `->` has higher precedence than `*`. `*naya -> data` is equivalent to `*(naya -> data)` https://en.cppreference.com/w/cpp/language/operator_precedence – Thomas Sablik Jun 02 '20 at 22:04
  • 2
    Also in C++ use `nullptr` in preference to `NULL` and use initializer lists, like `Node(int x) : data(x), next(nullptr) { };` – tadman Jun 02 '20 at 22:05
  • 2
    Unrelated, but good to know: [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Ted Lyngmo Jun 02 '20 at 22:12

1 Answers1

2

The reason it has to be written this way is because of operator precedence, which decides which operations are executed using which operands. You can see from that link that 'member access' or -> is bound before 'Indirection (dereference)' or *a by default. By enclosing the dereference operation in brackets you can specify that you want it to be bound first before the member access.

To make this more concrete, in your example naya is a pointer to a pointer. C++ by default tries to bind the operands for the member access operation first (i.e. naya->data). If we were to add brackets to make the order explicit here, it would look like this:

*(naya->data)

This has the effect of dereferencing naya and then looking for a data member variable in the dereferenced object. The dereferenced object is pointer though, so it has no data member. If it didn't error out there, it would go on to try and dereference the value of the data member (the * part of the expression).

When you put (*naya)->data, you are telling C++ that it should dereference naya with * specifically, not (naya->data) as it does by default. (*naya) is a pointer to a Node object, so -> will successfully find the data member.

Rob Streeting
  • 1,675
  • 3
  • 16
  • 27
  • 2
    "operator precedence" means the way that operands are associated with operators; not the order of evaluation. This is a common misconception, e.g. in `f() + g() * h()`, the `*` has higher precedence but the `f()` might be evaluated before the others . – M.M Jun 02 '20 at 22:36
  • Thanks for the clarification @M.M! I'll update the answer. – Rob Streeting Jun 03 '20 at 09:28