I\'m converting a header file for a DLL written in C to Delphi so I can use the DLL.
My question is what is the difference between
int* i
It's the same thing.
Some developers prefer the int* i
notation because this way the type of i is clearly "pointer to an int" whereas int *i
seems less readable.
int* i
, int * i
, int*i
, and int *i
are all exactly equivalent. This stems from the C compiler (and it's compatible C like systems) ignoring white space in token stream generated during the process of parsing the source code.
Some people prefer to use int* i
thinking that it makes the code cleaner (as the pointer directive is seen as part of the type in their minds). Some people prefer to use int *i
thinking that it makes the code cleaner (as the type is an int, and they have an "i pointer" to it). Neither technique actually does anything differently after the compiler runs through the source code. When in doubt, mimic the existing style.
It's an accident of C syntax that you can write either int *i
or int* i
or even int * i
. All of them are parsed as int (*i)
; IOW, the *
is bound to the declarator, not the type specifier. This means that in declarations like
int* i, j;
only i
is declared as a pointer; j
is declared as a regular int. If you want to declare both of them as pointers, you would have to write
int *i, *j;
Most C programmers use T *p
as opposed to T* p
, since a) declarations in C are expression-centric, not object-centric, and b) it more closely reflects declaration syntax.
As an example of what I mean by expression-centric, suppose you have an array of pointers to int and you want to get the integer value at element i
. The expression that corresponds to that value is *a[i]
, and the type of the expression is int
; thus, the declaration of the array is
int *a[N];
When you see the phrase "declaration mimics use" with regard to C programming, this is what is meant.
Points mentioned above highlight cases where "Type *" makes the most sense.
But a case where I personally found "Type*" makes intuitive sense/is more readable is when passing a reference to a pointer type. It is non-intuitive to a beginner when reading a variable as :
int *&x; // what is x here ?
as opposed to :
int* (&x); // A reference to a int* type
They are the same. The two different styles come from a quirk in C syntax.
Some people prefer int* i;
because int*
is the type of i.
Others prefer int *i;
because the parser attaches the star to the variable, and not the type. This only becomes meaningful when you try to define two variables on the line. Regardless of how you write it:
int* i,j;
int*i,j;
int *i,j;
in each of those, i
is a pointer to an int, while j is just an int. The last syntax makes that clearer, although, even better would be:
int j, *i;
or best yet:
int *i;
int j;
As far as C goes they both do the same thing. It is a matter of preference. int* i
shows clearly that it is an int pointer type. int *i
shows the fact that the asterisk only affects a single variable. So int *i, j
and int* i, j
would both create i
as an int pointer and j
as an int.