difference between int* i and int *i

前端 未结 8 1071
有刺的猬
有刺的猬 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 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.

提交回复
热议问题