明天考二级C语言机试,复习的时候发现自己基础知识有个漏洞哦哦哦哦,是指针数组和数组指针的:
1 void fun(char (*ss)[20])
2 {
3 int len = strlen(ss[2]);
char c = ss[2][3];
4 }
5
6 void main()
7 {
8 char ss[10][20];
....
9 fun(ss);
10 }
其中fun函数的参数ss便是一个数组指针.采用
char* ss
或
char* ss[]
都是错误的(可以用char ss[10][20]),首先编译上通不过,本质上的原因是:
int* a[]; a是指针数组.
int b[10][20], (*c)[20] = b; b,c都是数组指针,即:A pointer to an array.有时也称为“行指针”,因为通过它能直接引用b某一行的元素.
int d=3, *e = &d; e是指向普通单个元素的指针.
这就是三者的概念之分。
上面的fun不能使用指针数组,因为实参ss是数组指针,也不能使用普通指针,因为fun中对ss中的元素进行定位时,编译器规定了某些东西,如下:
(*c)[10]用于告诉编译系统,它是一个指针,指向一个长度为10的整型数组。这样在用指针访问其所指向的内存单元的时候就可以用*(*(c+i)+j)来表示b[i][j];
若使用普通指针e,就需用:*(e+3*i+j)来表示b[i][j].
最根本的一点是,数组指针和普通指针是不同级别的指针,如前面的e是个一级指针,指针移动的间距是一个整型,而c是二级指针,移动间距是十个整型。故我们能通过c[i]直接访问b的某行:
But, how will you declare a Pointer to an Array of Integers?
It will look like this:
int (*iMyVar)[10];
iMyVar is now a Pointer to an array of 10 integers.
By the way, what is the difference between Pointer to integer and pointer to an array of integers? Well, it is slightly out of the topic; In simpler terms,
int *p; p++; increments address in p by the size of integer whereas,
int (*ap)[10]; p++ ; increments address in p by (10*size of integer).
ap points to 10 element int array,
p points to an int.
The c++ programming language:
int v[]={1,2,3,4};
int *p1=v; //pointer to initial element (implicit conversion)
int *p2=&v[0]; //pointer to initial element
char v[]= "Annemarie ";
char *p=v; //implicit conversion of char[] to char*
可以看出,数组指针转化为普通指针(不同级别的转换),编译器那是做了手脚的.