您的位置:首页 > 编程语言 > C语言/C++

C语言(指针,数组)(附加)

2017-07-24 22:18 316 查看
#include<stdio.h>

void main()

{

//一维数组:
int buf[10]={1,2,3,4,5,6,7,8,9,10};

//下标法
printf("%d %d\n",buf[0],buf[9]);

//指针法:
printf("%d %d\n",*(buf+0),*(buf+9));

//指针变量:存储指针的变量
int* pbuf=buf;
printf("%d %d %d %d\n",*(pbuf+0),*(pbuf+4),pbuf[0],pbuf[4]);

//二维数组:
int score[4][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20}};

//引用:下标法,指针法
printf("%d\n",score[2][1]);

//行与列指针:
printf("%x %x %x %x\n",&score[0][0],&score[1][0],&score[2][0],&score[3][0]);
//行+n   指向第n行
printf("%x %x  %x  %x\n",score+0,score+1,score+2,score+3);
//列:  列+1指向下一列
printf("%x %x %x %x\n",*(score+0)+0,score[0]+1,score[0]+2,*(score+0)+3);
//打印元素:确定此元素的地址
printf("%d %d %d\n",score[2][2],(*(score+2))[2],*(*(score+2)+2));

//指针变量:
int* pscore=score;  
printf("%d",*(pscore+1));//说明:pscore+1指向下个元素

//二维数组指针:
int (*mp)[5]=score;
printf("%x %x %x %x\n",mp,mp+1,mp+2,mp+3);
//行指针
printf("%x %x %x %x\n",*(mp+0)+0,mp[0]+1,*mp+2,mp[0]+3);
//第一行列指针

//第3行  通过mp指针
int i=0;
for(i=0;i<5;i++)

printf("%d %d\n",mp[2][i],*(*(mp+2)+i));

//第二列

for(i=0;i<4;i++)
printf("%d %d\n",mp[i][1],*(*(mp+i)+1));

for(i=0;i<20;i++)
{
// printf("%d ",mp[i/5][i%5]);
printf("%d %d\n",*(pscore+i),pscore[i]);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  指针 C语言 数组