2012-09-26 284 views
0

我的老師讓我找出差異。任何人都可以幫助我? 其實我知道的第一部分是指針數組但什麼第二部分means.Both不一樣的,因爲我想爲這個代碼:int * x []和int(* x)[]之間的區別?

i = 1; 
j = 2; 
x[0] = &i; 
x[1] = &j; 

得到了一個錯誤說「左值要求」

+2

哪你指的是什麼語言? C? – Max

回答

3
int *x[3]; 

這裏x是一個指向int的3個指針的數組。

int (*x)[3]; 

這裏x是指向三個int秒的陣列。

下面是兩者的使用例:

int* arrayOfPointers[3]; 
int x, y, z; 
arrayOfPointers[0] = &x; 
arrayOfPointers[1] = &y; 
arrayOfPointers[2] = &z; 

int (*pointerToArray)[3]; 
int array[3]; 
pointerToArray = &array; 

HTH

0

int(*x)[ARRAY_SIZE]解釋作爲指針以一個整數數組。

7

如有疑問,請諮詢cdecl

int (*x)[] 
declare x as pointer to array of int 

int *x[] 
declare x as array of pointer to int 
+0

真棒網站:)謝謝。 – keyser

+0

+1偉大的網站! – Raj

2

通過近變量名「X」開始,以類型完成了您的工作方式,牢記Operator Precedence。意思是,在()和*之前的任何東西。

 x[] -- x is an array 
    *x[] -- x is an array of pointers 
int *x[] -- x is an array of pointers to ints 

(*x)  -- x is a pointer 
(*x) []  -- x is a pointer to an array 
int (*x)[] -- x is a pointer to an array of type int 
1

首先,請記住,C聲明反映表達(即,聲明模擬物使用)的類型。

舉例來說,如果你有一個指向整數,你要訪問的整數值被指向的,你取消引用與一元*操作的指針,就像這樣:

int x = *p; 

類型的表達*pint,所以指針p的聲明是

int *p; 

現在假設你有一個指向int的指針數組;訪問任何特定的整數值,則下標在數組中找到正確的指針和解除引用的結果:

int x = *a_of_p[i]; 

下標操作者[]具有比所述一元*操作者更高的優先級,所以表達式*a_of_p[i]被解析爲*(a_of_p[i]) ;我們正在取消引用表達式a_of_p[i]的結果。由於表達*a_of_p[i]int,陣列的聲明的類型是

int *a_of_p[N]; 

現在翻轉周圍;而不是指向int的指針數組,您有一個指向數組int的指針。要訪問一個特定的整數值,則必須取消引用指針第一然後標中的結果:

int x = (*p_to_a)[i]; 

由於[]的優先級高於*,我們必須用括號強制運營商的組合,這樣的標適用於表達式*p_to_a結果。由於表達(*p_to_a)[i]的類型是int,該聲明是

int (*p_to_a)[N]; 

當你看到一個聲明,看起來有點發毛,先從最左邊的標識和工作的方式了,記住,[]()具有更高的優先級比*,所以*a[]是指針的數組,(*a)[]是一個指向數組,*f()是返回指針的功能,並且(*f)()是指向一個函數:

 x  -- x 
    (*x)  -- is a pointer 
    (*x)[N] -- to an N-element array 
int (*x)[N] -- of int 

    x  -- x 
    x[N]  -- is an N-element array 
    *x[N]  -- of pointer 
int *x[N]; -- to int. 
相關問題