指针数组与数组指针
来源:互联网 发布:网络推广月工作计划 编辑:程序博客网 时间:2024/05/16 11:24
数组指针(指向数组的指针)是存放着地址的变量,属于二级指针,寻址方式是寄存器间接寻址,操作数(变量)存储在内存中,该内存地址(一级指针)存储在寄存器中,寄存器地址(二级指针)存储在地址码字段中,这种存储方式指令长度短,范围大,可是每次要访问两个设备,所以速度慢。
形式:数据类型 (*指针变量)[指向数组的长度];
用数组指针遍历二维数组非常方便。
arr[3][4]={{1,2,3,4},{5,6,7,8},{9,6,3,8}};
int (*p)[4];
int *q;
for(p=arr;p<arr+3;p++)
{
for(q=*p;q<(*p)+4;q++)
printf(“%5d”,*q);
printf(“\n”);
}
Out: 1 2 3 4
5 6 7 8
9 6 3 8
①、&arr[1][0]:二维数组第二行第一列元素的地址,等于*(a+1)+0;
②、*(arr+1)第二个一位数组的地址(也是第二个一位数组首元素的地址),等于arr[1];
③、arr[1]:第二个一位数组的名字,也是第二个一位数组的地址(也是第二个一位数组首元素的地址);
在数值上,①=②=③
指针数组本质上是数组,每个元素均为指针变量。指针数组可以作为函数参数使用,使用方法和一般数组一样。 如果各个元素指向不同的字符串,可以灵活的调用字符串。
形式: 数据类型 *指针变量名[数组长度];
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int aio(char *a[])
{
int i;
for(i=0;i<4;i++)
free(a[i]);
return 0;
}
int main(void)
{
char *a[3];
char b0[20]=" (*^__^*) ";
char b1[20]="O(∩_∩)O";
char b2[20]="~~(>_<)~~";
int n;
for(n=0;n<3;n++)
{
a[n]= (char*)malloc(100);
if(a[n]==NULL)
{
printf("fail\n");
exit(1);
}
//memset(a[n],'/0',sizeof(a[n]));
}
a[0] = b0;
a[1] = b1;
a[2] = b2;
//n=0;
for(n=0;n<3;n++)
{
printf("a[%d]=%s\n",n,a[n]);
}
getchar();
/*释放内存*/
aio(a);
return 0;
}
Out:a[0]=(*^__^*)
a[1]=O(∩_∩)O
a[2]= ~~(>_<)~~
- 指针:指针与数组
- 指针与指针数组
- 指针数组与数组指针
- 指针数组与数组指针
- 指针数组与数组指针
- 指针数组与数组指针
- 指针数组与数组指针
- 指针数组与数组指针
- 指针数组与数组指针
- 数组指针与指针数组
- 指针数组与数组指针
- 指针数组与数组指针
- 指针数组与数组指针
- 指针数组 与数组指针
- 指针数组与数组指针
- 数组指针与指针数组
- 指针数组与数组指针
- 指针数组与数组指针
- smartforms如何取消word为默认编辑器
- CGLib动态代理
- 面试题24:二叉搜索树的后序遍历序列
- openCv4Android之fitLine中各算法总结
- 小混混之自我提高篇——目标
- 指针数组与数组指针
- vmlinux,vmlinuz,bzimage,zimage,initrd.img uimage的区别与联系
- java学习之集合框架工具类
- Linux线程互斥锁
- Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor
- 用命令行运行hadoop程序WordCount
- 找到一篇关于form以post、get方式提交数据中文乱码问题总结
- Web前后端分离:MVC简介
- android自定义View实现图片上传进度显示(仿手机QQ上传效果)