There is an other.
int * p;
Here is an otter
:)
i am in this picture and i do like it
Having an asterisk both be the type indicator and the dereference operator is one of the great programming language design blunders of our time, along with allowing nulls for any type in so many languages.
I also sometimes wish that the syntax in
if
statements was inverted, where()
was optional and{}
was required.Rust makes this choice and it is way better.
Can you give me an example? I’m not sure I follow. Might be language specific?
if(condition) statement; Is valid in typical C-style syntax.
if condition { … }
Is invalid in typical C-style syntax
Having assignments return a value is right up there as well.
Because of the possibility of accidentally performing an assignment in a conditional expression?
If yes, I agree that it’s not great.
The fact it’s a pointer is part of the type, not part of the variable name. So
int* p
is the way.You would think so, but
int* a, b
is actually eqivalent toint* a; int b
, so the asterisk actually does go with the name. Writingint* a, *b
is inconsistent, soint *a, *b
is the way to go.Yeah, and I’d say that’s a design flaw of the language as it is unintuitive behaviour.
While technically true, that’s also one of the worst ‘features’ of the language and I personally consider it a bug in the language. Use two lines and make it clear and correct.
When people say “pointers are hard”, they mean “I have no idea where the star goes and now an ampersand is also implicated”.
That’s the part where you give up and randomly shove/unshove symbols in until the code works.
I’ve definitely never been guilty of this. /s
Alright, I’ll never, ever write something this way now. Good to know.
Don’t declare more than 1 pointer per line. This resolves that, badly.
int* i, j
The C syntax is just messed up.
Int *p is unreadable, unreasonable, and bad programming.
Why so? It’s what it actually is
But C syntax clearly hints to
int *p
being the expected format.Otherwise you would only need to do
int* p, q
to declare two pointers… however doing that only declaresp
as pointer. You are actually required to type*
in front of each variable name intended to hold a pointer in the declaration:int *p, *q;
That’s because C was designed by a fool.
The fools who created Unix
And C!
std::shared_ptr<int> p;
I’m just a c# dev wishing to fuck we had something visual to indicate reference types so my coworkers could stop misusing them
Wait until I tell y’all about const.