tags:

views:

704

answers:

7

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

and

int *i

I convert the first to

i: PInteger;

But i'm not sure what the correct conversion is for the second one in Delphi.

from my understanding the first is a simple typed pointer. The second is a pointer variable. but i'm not sure what the difference is.

+20  A: 

int* i and int *i are completely equivalent

Vladimir
So the difference in the DLL header is a lack of consistency or a typo. thanks
MikeT
If you're going to take the time to thank the guy, could you accept his answer, too?
Steve M
sorry but I couldn't accept the answer straight away due to the question being answered so quickly. i got a msg telling me i can only accept the answer in 11 mins time
MikeT
Ah, cool. I didn't know about that limitation.
Steve M
Also, and this is a cause for concern here at SO: When you accept an answer to a question, people stop viewing the question. I've noticed this several times. If I ask a question, and leave it open for a while, many people view it, and several will try to answer it. If I get a decent answer early on and accept it, there's a dramatic drop in viewers. There's also an abrupt stop in answers, so maybe there are even better answers to the question that will never surface. Food for thought...
Svein Bringsli
+3  A: 

There is no difference. You could type it as int * i if you wanted; they all mean the same thing: "i is a pointer to an int."

Steve M
+30  A: 

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.

Kyle
+1 for explaining the `int* i, j` caveat
pmg
think I actually knew this already but its over 10 years since I did any C/C++ coding.
MikeT
Din't you love those language quirks :-) I'm so glad I had completely forgotten about that, but it is good to see it noted somewhere with a high rating.
Jeroen Pluimers
+1  A: 

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.

Colin Hebert
A: 

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.

Edwin Buck
+7  A: 

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;
James Curran
+4  A: 

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.

John Bode