anyway, with this in mind, can someone explain something to me? in C, i tend to write this:
int* x;
i write it like that because i believe i am bringing into existence x, whose type is "a pointer to an integer". it's not an int, it's an int pointer. so i write int*, because that's what type it is. it seems clearer that way. however i often see this:
int *x;
this seems less clear, but some (most?) people obviously think THAT way is the clearer way. if you do, what's your thought process?
my hottest take here is probably this:
int_ptr a;
printf("%x\n", a); // 0x0
printf("%d\n", sizeof(int_ptr)); // 8
a = stalloc(sizeof(int_ptr));
printf("%x\n", a); // 0x02BF26A...
poke(a, 10);
printf("%d\n", peek(a) ); // 10
@jk Consider this:
int* a, b;
This declares one pointer and one integer, which doesn't agree with the way you were thinking of it in your first example.
@jk Like you said, it's a matter of preference in a simple declaration like you showed, but it might lead one to thinking of '*' in a way different from how the language technically views it, so I'd discourage a beginner from using that form. It's strictly an operator that binds to the declarator, not the type specifier.
One of the great things about C is that it gives you a ton a freedom as long as you know what you're doing!
@jk
I write C like this thinking int = *x, i.e., x when dereferenced gives an int.
*x is an int, rather than x is an int pointer.
The two ways of thinking get mixed up with each other though… casting is (int *) x, usage is *x…
@jk
The former makes more sense in a vacuum, but the latter is much less surprising since the “pointer-ness” is tied to the variable name rather than the type.
So
int* x, y;
gives you an int* x and a plain int y, not two pointers. Wheras
int *x, *y;
indicates that the pointer is tied to each name rather than the type.
(Not that it'd be particularly good practice to declare a pointer and a non-pointer in the same statement, but it still helps avoid mistakes)
@[email protected] because thereafter `*c` has type `char`
int *a, *b, *c; instead of int* a, b, c; or else b and c are not pointers at all.@jk it's commonly taught that way for the reasons other folks have explained (it's how I was taught for sure). though I have seen a third option where the asterisk has whitespace on both sides like int * a; lol
if you're curious, the entire clusterfuck that is declaration syntax can be seen here: https://en.cppreference.com/w/c/language/declarations
@jk I thought it was extremely confusing. The object type is a pointer to an integer, not an integer, so wouldn't it be attached to the type specifier?
Honestly hate "int *p"
@senj @shram86 @jk Right, "int *x" is how the language _actually works_. It may not be how we wished it worked, but that is how it works, so it is clearer to write things that way.
If you designed C now, you might design it so that "int* x" is how it works. But that would be a slightly different language.
@WAHa_06x36 @senj @jk does this mean that when parsed, every C interpreter just transposes the asterisk?
Every compiler I've ever used doesn't care where it goes. I know K&R do it one way, but practice is different....
@WAHa_06x36 @senj @jk I'm still unsure of what you mean by "the asterisk belongs to the variable name".
By that logic you can have a int *x and an int x and C wouldn't care but I'm pretty sure it does?
@jk I disagree with the C take on this, but the idea is that the value itself resides at the actual destination pointer address, rather than being the actual stack or heap variable's value stored in the variable. So the variable is "pointer-to-x", with the type of x being int.
But imo, they're wrong, and I always write it assuming the value stored *right here in the variable* is the type I care about, and that's `int* x`