difference between int* i and int *i

前端 未结 8 964
有刺的猬
有刺的猬 2020-12-01 07:31

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

相关标签:
8条回答
  • 2020-12-01 07:58

    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.

    0 讨论(0)
  • 2020-12-01 08:01

    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.

    0 讨论(0)
  • 2020-12-01 08:02

    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.

    0 讨论(0)
  • 2020-12-01 08:07

    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 讨论(0)
  • 2020-12-01 08:08

    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;
    
    0 讨论(0)
  • 2020-12-01 08:11

    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.

    0 讨论(0)
提交回复
热议问题