struct dirent *entity difference

closed account (GTXEhUp4)
struct dirent *entity

struct dirent* entity i runned the both codes and complair didn't give any error. if both of these code true why people prefer struct dirent* entity it's quite confused ...
Last edited on
i runned the both codes


complair didn't give any error


@baduymus - you need to explain your problem more accurately. You probably need a better translator. "Both codes" implies two codes, neither of which we can see. "Complair"??? - compiler ?




why people prefer struct dirent* entity

Not sure where you are getting your statistical data on that from.

Any of
dirent* entity

dirent *entity

dirent * entry

dirent*entry
could equally well be used, according to people's taste and the time of day.

But you don't need the word "struct" in c++.

Last edited on
Since the position of the space in that case isn't relevant it is rather a matter of taste...
If dirent is a struct, then in C++ you can refer to just dirent without prefixing by struct. In c, you need to use struct dirent.
Lots of people prefer dirent* entity because the type of entity is dirent* (pointer to dirent).

But dirent *entity is common too, especially in C, but for other (technical) reasons...
Last edited on
Don't forget that the * relates to the variable and not the type. So:

 
dirent* entity, ent1;


has entity as a pointer to dirent, but ent1 is of type dirent and is not a pointer.

if both are required to be a pointer then:

 
dirent* entiry, *ent1;


Stroustrup:
A "typical C programmer" writes "int *p;'' and explains it "*p is what is the int" emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.

A "typical C++ programmer" writes "int* p;" and explains it "p is a pointer to an int" emphasizing type. Indeed the type of p is int*. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.

The critical confusion comes (only) when people try to declare several pointers with a single declaration:
...
Stick to one pointer per declaration and always initialize variables and the source of confusion disappears.

https://www.stroustrup.com/bs_faq2.html#whitespace
Stick to one pointer per declaration and always initialize variables and the source of confusion disappears.

Solid advice.

Some of these are more confusing than others:
1
2
3
4
dirent* entiry, *ent1;
dirent* entiry,* ent1;
dirent *entiry, *ent1;
dirent*entiry,*ent1;
I think the one-variable-per-declaration convention has a lot of merit even without pointers. It makes the code easier to read and it also makes the code easier to work with, e.g. if I need to (re)move one of the variables or add another one.

The advantage is similar to the convention of always using { and } with loops and if statements even when not needed because it makes it easier to add more statements later and the consistency makes the code easy to read.

The only place I would consider declaring more than one variable in the same declaration would be a for loop.

Maybe I would have felt differently if I had been programming in old C where I would have had to declare all variables at the beginning of the function before having a sensible value to initialize them with, but I think the points I made above hold true even here to a large extent especially if I would want to put a comment on each variable to explain where and what it's used for (something that is less obvious if the declaration is located far from where the variable is used).

And for anyone that feels it's tedious to retype the name of the datatype (and also the "struct" keyword in C) and possibly other qualifiers like const I should perhaps mention that I don't actually retype lines like that. I press Ctrl+D to duplicate the line and then I just change the variable name. To remove a declaration I just place the caret at the end of the line and press Shift+Home followed by Delete. Easy, peasy! :)
Last edited on
The source of confusion with multiple pointers to be declared seems to be an unfortunate artefact of the language.

In Fortran it would just be
integer, pointer :: p, q, r, s

or, translating the example given:
type(dirent), pointer :: entiry, ent1

I would be interested in knowing what the (relatively few) other languages that use pointers do.

FWIW, I tend to use dirent *entiry, but then I never coded in C.
C++ (if, for some obscure reason, we must have multiple pointers per declaration):

1
2
3
4
5
6
7
8
template < typename T > using pointer = T* ;

int main()
{
    pointer<dirent> p1 = nullptr, p2 = nullptr, p3 = nullptr ;
    pointer<int> p4 = nullptr, p5 = nullptr ;
    // etc.
}
As a matter of interest, what (if any) initialisation is done by
int *p{};
p is set to nullptr
Topic archived. No new replies allowed.