What do you think about the difference between the following declarations? Which do you prefer?
int* p;
int *p;
It's often said the first is C++ style and the second is C style. I prefer C but conceptually the first declaration makes a lot more sense to me. You're defining a variable p of type int*. p is an int*. You dereference it, *p, when you want the int. Makes sense. The second example to me seems like gobbledygook, but it's the way the C authors thought of it, because of this bullshit:
int *p, *q;
So what the fuck is the deal? Can someone who uses "int *p" syntax explain to me what goes through your head when you write that garbage?
If the calling convention is cdecl, the number of parameters that are passed is irrelevant, as the caller does the cleanup, and the callee can just look up as many parameters up the stack as needed. Other conventions such as stdcall aren't as friendly with variable numbers of arguments. The fact is that as long as main's calling convention is cdecl and you use the parameters you're receiving in the correct way(assuming certain parameters), you'll be perfectly fine. Some uses may not be "comforming" to the standard, but in implementation-specific cases, it will work perfectly.
Well, technically, it probably should be int *p, because the dereferencing operator (aka the pointer asterisk) is not part of the type, but a per-variable specifier. Think of it like an initialization:
// a and b should both be initialized to 1
// The WRONG way:
//int a, b = 1;
// The RIGHT way:
int a = 1, b = 1;
So basically, you're specifying that both of the variables are pointers, but individually. It's more intuitive for multi-variable declarations as well, which is why it is seen more in C, because the common style for structs uses multi-var declarations wherever it can.
But personally, I prefer int* a. And I'm impartial between char** argv and char *argv[], but the latter just seems unwieldy.
>>128
If you think of it that way, then you're wrong.
Whereas initialization looks like char *a = "something";, later on if you want to assign to a, you don't write *a = "something else";, you write a = "something else";.
Name:
Anonymous2009-10-30 18:59
>>130
that's only because a string literal is an array of characters. you're assigning the address to a not the actual value.
try modifying the values that a points to.
for instance a[0] = 'b'; SEGFAULT.
all pointers are represented inside the machine exactly the same. void *;
char *;
int ******************;
struct whatever ******************************;
all exactly the same internal representation.
the type is what the pointer points to, each asterisk is a just representation of a layer of indirection - it's there to make the code easier to read for the programmer; the machine couldn't care less about it.
>>131
He was referring to the fact you modify a pointer without the asterisk, I believe. (IHBT)
Name:
Anonymous2009-10-30 20:28
what it basically boils to down to is nubbins seem to thing that * in this context means ZOMG POINTER, but it actually means "dereference" and so char *p is define dereferenced p as char.