用指針變量引用二維數組的元素,相對複雜些,當指針變量指向數組時,既可以是指向行,也可以是指向列。當指針變量指向列,如果把指針變量當成數組名,就可以把二維數組當成一維數組來對待,一維數組元素的個數由二維數組的行數與列數的乘積得到。如下代碼:
void main()
{int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
int (*p)[4];
p=a;/*p指向行 */
printf("%d
",*(*(p+1)+2));
printf("%d
",p[1][2]);
}
程序運行後輸出a數組中a[1][2]元素的值。
void main()
{int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
int *p;
p=a[0];/*p指向列 */
printf("%d
",*(p+6));
printf("%d
",p[6]);
}
程序運行後也是輸出 a[1][2]元素的值[3,6-7]。
9指向指針變量的指針變量
聲明指向指針變量的指針變量時,有如下格式:int **p;可以認為p前麵的第一個“*”號標誌p是一個指針變量,第二個“*”號標誌p是一個指向指針變量*p的指針變量,而*p是指向一整型變量的指針變量。*p是一級指針變量,p是二級指針變量,即p代表的存儲單元裏存儲著*p的指針。
10結語
指針類型是C語言的一個重要組成部分,使用指針類型編程有許多優點,因此學習掌握指針、指針
類型、指針常量、指針變量、指針變量類型、指針表達式、數組、行指針、列指針、指針的指針等概念對深刻理解、掌握和應用指針類型非常關鍵。近幾年來我們對C語言教學嚐試使用按上述關鍵講解指針知識,實踐證明學生更容易理解、掌握和應用,特別是等級考試中的指針知識題型錯誤率降低很多,取得了很好的教學效果。
參考文獻:
[1] 徐建民,王鳳先. 彙編語言程序設計[M]. 北京:電子工業出版社,2001.
[2] 肖金立. 微型計算機原理與應用[M]. 北京:電子工業出版社,2001.
[3] 譚浩強. C程序設計[M]. 3版. 北京:清華大學出版社,2005.
[4] 劉禕瑋,汪曉平. C語言高級實例解析[M]. 北京:清華大學出版社,2004.
[5] 徐金梧,楊德斌,徐科. TURBO C實用大全[M]. 北京:機械工業出版社,1996.
[6] 景紅,王國強,尹治本 . C語言程序設計教程[M]. 成都:西南交通大學出版社,2000.
[7] 黃維通,關繼來,戢彥泓 . C語言程序設計(二級)[M]. 北京:機械工業出版社,2000.
Study of Pointer Type in C Language
YANG Jia-yi, ZHUANG Li-juan
(Applied Information Technology Department, Institute of Technology, Jiamusi University, Jiamusi 154007, China)
Abstract: From my college student a decade in the National Computer Rank Examination (C language part) situation, and the kinds of questions by analyzing the distribution of the relationship between performance indicators in the kinds of questions found in the highest rate of loss of points, in this based on the combination of teaching practice, this paper presents pointer to the knowledge of learning methods and sequence of the process used in C Programming teaching has received good results.
Key words: NCRE; pointer; pointer type; pointer constant; pointer variable; arrays and pointers; row pointer; column pointer
(編輯:姚彥如)