There is an other.
int * p;
Here is an otter
:)
i am in this picture and i do like it
Male otters kidnap children otters and hold them for ransom until the mum gives them food.
I’ve seen that in style guides ”because it should piss of everyone equally”.
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.
Based
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.
Yeah, exactly that.
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.
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
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.
Don’t declare more than 1 pointer per line. This resolves that, badly.
Alright, I’ll never, ever write something this way now. Good to know.
This is true in C, but not in D.
Then again, at least in C, the mantra is “declaration follows usage”. Surely you don’t write pointer dereferences as
* ptr
? Most likely not, you most likely write it as. The idea behind the
int *ptr;
syntax is basically that when you do, you get an
int
.And with this idea, stuff like function pointers (
int (*f)(void)
), arrays of pointers (int *a[10]
) versus pointers of arrays (int (*a)[10]
) etc. start making sense. It’s certainly not the best way to design the syntax, and I’m as much a fan of the Pascal-styled “type follows the identifier” syntax (e.g.let x: number;
) as anyone, but the C way does have a rhyme and a reason for the way it is.int* i, j
The C syntax is just messed up.
deleted by creator
It’s part of the type yet it’s also a unique identifier. That’s the whole thing with east or west
const
.const int *
is aimmutablemutable pointer that points tomutableimmutable memory.int *const
is amutableimmutable pointer that points toimmutablememory.int const *
is the same type as the first example, aimmutablemutable pointer that points tomutableimmutable memory.Same stuff applies to references which makes it easier to think of the variable owning the
*
or&
as if you want that pointer or reference to beconst
it has to go after.Edit:I am a moron who managed to get it exactly backwards :|
Found the guy that can probably do function pointers!
I wrote a couple unholy lines of C++ the other day using the ternary conditional operator to select a class member function to be called with a fixed argument.
I think my teammates were too scared to call me out on it.
KISS, my guy.
It’s actually simpler than doing it the “right” way, but I wanted to see how much I could make C++ pretend it was Python.
Lol yeah. I don’t even really write C++ but I sure as shit know a bunch of syntax and junk haha
I think you’ve got it backwards. I learned to read pointer decls from right-to-left, so
const int *
is a (mutable) pointer to an int which is const whileint *const
is a const pointer to a (mutable) int.Fuck me man that’s what I get for writing that just before bed
I always read it right to left and it seems to make sense to me.
I do this in my code because it looks better and makes more sense…until I decide to declare 2 vars on one line and then I use the very cursed
int* a, *b
I just wouldn’t do that.
tbh I always think about it as ‘p’ is a pointer to int
therefore *p is an int
therefore I should call it int *p;
however, of course, you should use what your team prefers. Having good yet inconsistent style is worst than mid consistent style.
And yet the default clang formatter gets it wrong.
I don’t code much C++, but then I’d lose alignment with:
x = *p;
and I feel that would bug me.I’m looking at Google Style Guide for my next project and it says either is fine, just don’t declare more than one per line.
std::shared_ptr<int> p;
Wait until I tell y’all about const.
I’m just a c# dev wishing to fuck we had something visual to indicate reference types so my coworkers could stop misusing them
oh thank you! I use jetbrains but I wonder if I can implement the same thing
It’s such a short list of value types though. How can they have that much trouble? All of the various ints and floats, bool, char, structs, and enums. Everything else is reference.
no int * ptr fans here?
Ever written a entire php in one line? I did, and that without separation whenever possible.
Man my coworker even ended the trolling contest over that.
Int *p is unreadable, unreasonable, and bad programming.
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!
What, could you have done better in 70-whatever?
Why so? It’s what it actually is