0
struct linklist
{
    int data;
    struct linklist *next;
}

Why using struct linklist in front of *next? One can create a pointer simply by *node?

hellow
  • 12,430
  • 7
  • 56
  • 79
Aman Kashyap
  • 25
  • 1
  • 7

2 Answers2

0

Why we are using struct linklist in front of '*next'

You don't need to use an elaborated type specifier here. So I suppose that you use it because you can.

Or perhaps you intend to use the struct across language boundaries, in which case you use it because it is necessary to use it in C.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

It is possible to have a function and a struct with the same name. To keep them apart you have to use the keyword struct.

E. g.

struct l {
    int b;
};

void l(struct l &a) {
    a.b = 5;
}

int main () {
    struct l a;
    l(a);

    return 0;
}

In this case you can't leave out the keyword struct. But this is a special case that you may find in older source codes.

Also in c you need to use the keyword. So

struct l {};

int main() {
    struct l a;
}

is valid c and c++ code but

struct l {};

int main() {
    l a;
}

is only valid c++ code.

Usually you don't need that keyword in c++.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • It is not uncommon in C to use a `typedef` after the struct definition to avoid having to use the `struct` keyword everywhere the typename is used, eg: `typedef struct l l;` then you can use `l a;` instead of `struct l a;` – Remy Lebeau Oct 18 '18 at 16:47
  • @RemyLebeau, yes, you are completely right. But the question was when and why to use the keyword `struct` in c++. I had to use it about 2 weeks ago because I worked with an older c library that had a function and a struct with the same name. – Thomas Sablik Oct 19 '18 at 09:19