Question about pointer objects

Question about pointer objects

Hi guys!

I am trying to study and understand something…


Take this line….

struct dirent *ent;


That lines requires dirent.h to be included and is obviously a pointer object for the dirent struct. My question is, why put “struct” at the start of the line?

In a few examples I made, I was able to make code that did what I wanted whether I used..

struct dirent *ent;
or
dirent *ent;

So I am trying to understand why I see some programs that put struct or class at the front of some pointer objects.

Thank you in advance!
In C, without using a typedef, you had to put 'struct name' instead of just 'name' when declaring an object.
In C++, this is no longer required, but you might still see C-style code that is compatible with C++ use it.
In C, when you do struct T{}; you're really only declaring 'struct T', and to use it you need to put the struct always. You can only omit it if you do either
 
typedef struct T T;
or
1
2
3
typedef struct T{
    //members
} T;

C++ ignores this silliness and just treats 'struct T' and 'T' as equivalent.
struct foo
{
things
} variable; //this makes a variable of type foo. you can do it to classes as well. It is likely a global variable, due to where you typically create structs or classes, which is at the global scope. I really dislike this style (its left over from C) because of the accidental globals as wells as being hard to spot when trying to get a list of variables out of the code.

Topic archived. No new replies allowed.