43

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.

Mike Taylor
  • 2,376
  • 2
  • 17
  • 33
  • possible duplicate of [In C, why is the asterisk before the variable name, rather than after the type?](http://stackoverflow.com/questions/398395/), [What's your preferred pointer declaration style, and why?](http://stackoverflow.com/questions/377164/) – outis Dec 20 '11 at 22:40

9 Answers9

71

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,978
  • 1
  • 18
  • 30
40

int* i and int *i are completely equivalent

Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • So the difference in the DLL header is a lack of consistency or a typo. thanks – Mike Taylor Sep 22 '10 at 14:24
  • If you're going to take the time to thank the guy, could you accept his answer, too? – Steve M Sep 22 '10 at 14:36
  • 22
    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 – Mike Taylor Sep 22 '10 at 14:39
  • 8
    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 Sep 23 '10 at 06:14
20

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
  • 101,701
  • 37
  • 181
  • 258
15

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
  • 119,563
  • 19
  • 122
  • 198
4

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
  • 8,246
  • 2
  • 25
  • 26
3

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
  • 69,361
  • 7
  • 100
  • 138
2

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
  • 91,525
  • 15
  • 160
  • 151
0

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

0

It is different as:

  1. When you want to declare many pointers on one line without writing * every time then you can use int* a,b,c,d; Here, a,b,c,d will all be pointers to integer

  2. When you want to declare a pointer and a normal variable in one line, you must use int *a,*b,c,d; Here, a and b are pointers to integer, c and d are integer variables.

  • "Here, a,b,c,d will all be pointers to integer" is wrong, only the var 'a' will be a pointer. So, it is better put the asterisk before the var name than next to the data type, to avoid confusion. – SimoX May 19 '23 at 20:28