指针的大小是固定的,在32位平台是4字节,在64位是8字节;
指针是有类型的,类型决定了指针“+1”的步长,和指针解引用时候的权限。
1.字符指针
#include "stdafx.h"
int main(int argc, char* argv[])
{
char *pc="hello sheep";//常量字符串
printf("%c\n",*pc);//本质上是把这个字符串的首地址存储在了 pc 中
return 0;//指针可指向字符串
}
运行结果:
相比较
int main(int argc, char* argv[])
{
char *pc="hello sheep";
printf("%s\n",pc);// 打印字符串用%s
return 0;
}
运行结果:
2.指针数组
存放指针的数组,指针数组的所有元素都必须具有相同的存储类型和指向相同数据类型
#include "stdafx.h"
int main(int argc, char* argv[])
{
int a[5]={1,2,3,4,5};
int b[5]={2,3,4,5,6};
int c[5]={3,4,5,6,7};
int *arr[3]={a,b,c};
int i=0;
for(i=0;i<3;i++)
{
int j=0;
for(j=0;j<5;j++)
{
printf("%d",*(arr[i]+j));//等价于 printf("%d",arr[i][j]);
}
printf("\n");
}
return 0;
}
运行结果: