Home  >  Article  >  Database  >  c语言中的int(*p)[5]类型分析

c语言中的int(*p)[5]类型分析

WBOY
WBOYOriginal
2016-06-07 15:56:382041browse

#includestdio.h int main() { int i; int b[5]={1,3,5,7,9}; int (*a)[5] = b; int *m = a; //a范围内的空间按照 int大小来取 for(i = 0;i5;i) { printf(%d\n,m[i]); } return 0; } 输出结果为 1 2 3 4 5 其中 int (*a)[5] 表示在栈中产生一个大小为 5个int

#include
int main()
{
int i;
int b[5]={1,3,5,7,9};
int (*a)[5] = &b;
int *m = a; //a范围内的空间按照 int大小来取值
for(i = 0;i {
printf("%d\n",m[i]);
}
return 0;

}

输出结果为 1 2 3 4 5

其中 int (*a)[5] 表示在栈中产生一个大小为 5个int的空间 a代表的的是整个空间的首地址

int *m = a;定义的是它的取值偏移量,也就是说一个整形指针*m 首地址为开辟的a的空间的首地址

为更好分析下面的程序就能让你更能懂了:

#include
int main()
{
int i;
short b[5]={1,3,5,7,9};
int (*a)[5] = &b;
short *m = a; //a范围内的空间按照 int大小来取值
for(i = 0;i {
printf("%d\n",m[i]);
}
return 0;
}

输出结果为 :1 2 3 4 5

由(*a)[5]产生一个空间大小为5个int的空间,空间的首地址和数组的首地址相同,只是一个空间而已没有定义其取值方式,由short *m = a;表示取其空间大小为short的类型,这个类型和int (*a)[5]没有关系,int (*a)[5]值负责产生空间,没有类型,有点类似于malloc 函数。

仔细推敲有助于深入理解。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn