-1

I created a very simple program of linked lists in C.

#include<stdio.h>
#include<stdlib.h>

int main(){
    struct Int{
        int num;
        struct Int *ptr;
    };
    typedef struct Int NODE;
    NODE *start;
    start = (NODE *) malloc(sizeof(NODE));
    (*start).num = 100;
    (*start).ptr = (NODE *) malloc(sizeof(NODE));

    (*start).(*ptr).num = 123;
    (*start).(*ptr).ptr = NULL;

}

When I replace the last two lines as :-

start -> ptr -> num = 123;
start -> ptr -> ptr = NULL;

The error solved.

The question is that why I can't use (* start). instead of start -> .According to this answer What does this mean "->"? both of them are same.

Community
  • 1
  • 1
Sahib Navlani
  • 149
  • 1
  • 12

3 Answers3

11

you should write the last two lines as:

(*(*start).ptr).num = 123;
(*(*start).ptr).ptr = NULL;

because (*ptr) is not a member of (*start), you should access ptr then dereference the whole expression.

Mike
  • 8,055
  • 1
  • 30
  • 44
4

*ptr is not a member of struct Int - ptr is, so you can't have *ptr following the . operator.

If you absolutely have to use * and dereference your pointers, you need to treat the entire (*strart).ptr expression as a pointer:

(*(*start).ptr).num = 123;
(*(*start).ptr).ptr = NULL;
Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

N3337 5.2.5/1,2

A postfix expression followed by a dot . or an arrow ->, optionally followed by the keyword template (14.2), and then followed by an id-expression, is a postfix expression.[...]

In either case, the id-expression shall name a member of the class or of one of its base classes.[...]

So dot has to be followed by name of the class member. You can't even write:

A a;
a.(some_member);//note the parentheses
PcAF
  • 1,975
  • 12
  • 20