1.指针数组和数组指针:
指针数组是数组,数组的每个元素是指针
数组指针是指针,指针指向一个数组
1.指针数组:操作字符串数组
2.数组指针:在C语言中二维数组传参传数组指针和长度
int *a[5]; //指针数组
定义数组a,占40个字节空间,共有5个元素,每个元素为int*型指针
#include <stdio.h>
#include <string.h>
int main(void)
{
char *pstr[5] = {"hello", "world", "how", "are", "you"};
char *ptmp = NULL;
int i = 0;
int j = 0;
for (j = 0; j < 4; j++)
{
for (i = 0; i < 4-j; i++)
{
if (strcmp(pstr[i], pstr[i+1]) > 0)
{
ptmp = pstr[i];
pstr[i] = pstr[i+1];
pstr[i+1] = ptmp;
}
}
}
for (i = 0; i < 5; i++)
{
printf("%s\n", pstr[i]);
}
return 0;
}
int (*p)[5]; //数组指针
定义一个指针变量p,占8个字节空间, 指向一个int类型5个元素的数组的指针,指向20个字节空间
2.指针数组:
字符型指针数组:
主要用于操作字符串数组
练习:定义一个指针数组,并初始化为12个月英文字符串首地址,从终端输入1 - 12对应的数字,
打印出其对应的英文形式
Jan 1
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
#include <stdio.h>
int main(void)
{
char *pstr[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int n = 0;
scanf("%d", &n);
printf("%s\n", pstr[n-1]);
return 0;
}
字符型二维数组:
主要用于存放字符串数组
3.数组指针:
int (*p)[5] = NULL;
一维数组中数组的数组名是指向数组第一个元素的指针
int a[5];
a == &a[0]
可以把a理解为int *型(除了以下两种情况):
1.sizeof运算时
sizeof(a) = 20
sizeof(int *) = 8
2.&运算时
&a == int (*)[5]
&int * == int **
数组指针的特性:
1.对一维数组&得到数组指针
2.数组指针取*操作:值不变,类型降级成为指向数组第一个元素的指针
3.数组指针变量量为整个数组大小
4.数组和指针的关系:
1.一维数组和指针的关系:
int a[5] = {1, 2, 3, 4, 5};
int *p = NULL;
p = &a[0];
p = a;
访问数组第n个元素的方式:
a[n] == *(a+n) == *(p+n) == p[n]
#include <stdio.h>
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
printf("========================\n");
printf("&a[0]:%p\n", &a[0]);
printf("&a[1]:%p\n", &a[1]);
printf("&a[2]:%p\n", &a[2]);
printf("&a[3]:%p\n", &a[3]);
printf("&a[4]:%p\n", &a[4]);
printf("========================\n");
printf("a:%p\n", a);
printf("a+1:%p\n", a+1);
printf("========================\n");
printf("&a:%p\n", &a);
printf("&a+1:%p\n", &a+1);
printf("========================\n");
printf("*a:%d\n", *a);
printf("*(a+1):%d\n", *(a+1));
printf("========================\n");
printf("*&a:%p\n", *&a);
printf("*&a+1:%p\n", *&a+1);
printf("========================\n");
printf("**&a:%d\n", **&a);
printf("*(*&a+1):%d\n", *(*&a+1));
return 0;
}
2.二维数组和指针的关系:
int a[2][3] = {1, 2, 3, 4, 5, 6};
int *p = NULL;
int (*q)[3] = NULL;
p = &a[0][0];
p = a[0];
p = *a;
q = a;
#include <stdio.h>
int main(void)
{
int a[2][3] = {1, 2, 3, 4, 5, 6};
printf("=========================\n");
printf("&a[0][0]:%p\n", &a[0][0]);
printf("&a[0][1]:%p\n", &a[0][1]);
printf("&a[0][2]:%p\n", &a[0][2]);
printf("&a[1][0]:%p\n", &a[1][0]);
printf("&a[1][1]:%p\n", &a[1][1]);
printf("&a[1][2]:%p\n", &a[1][2]);
printf("=========================\n");
printf("a:%p\n", a);
printf("a+1:%p\n", a+1);
printf("=========================\n");
printf("a[0]:%p\n", a[0]);
printf("a[0]+1:%p\n", a[0]+1);
printf("a[1]:%p\n", a[1]);
printf("a[1]+1:%p\n", a[1]+1);
printf("=========================\n");
printf("*a:%p\n", *a);
printf("*a+1:%p\n", *a+1);
printf("=========================\n");
printf("**a:%d\n", **a);
printf("=========================\n");
printf("*(*a+1):%d\n", *(*a+1));
printf("=========================\n");
printf("*(*(a+1)+1):%d\n", *(*(a+1)+1));
return 0;
}
数组的数组名是指向数组第一行所有元素的数组指针
访问数组第m行第n列元素的方式:
a[m][n]
*(a[m]+n)
*(*(a+m)+n)
*(p+m*N+n)
*(*(q+m)+n)
*(q[m]+n)
q[m][n]
#include <stdio.h>
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int *p = NULL;
p = a + 2;
printf("*p = %d\n", *p);
p = (int *)(&a + 1) - 1;
printf("*p = %d\n", *p);
return 0;
}
5.二维数组传参:
int a[2][3];
int fun(int (*parray)[3], int len);
#include <stdio.h>
int OutputArray(int (*parray)[3], int len)
{
int i = 0;
int j = 0;
for (j = 0; j < len; j++)
{
for (i = 0; i < 3; i++)
{
printf("%d ", parray[j][i]);
}
printf("\n");
}
return 0;
}
int InputArray(int (*parray)[3], int len)
{
int i = 0;
int j = 0;
for (j = 0; j < len; j++)
{
for (i = 0; i < 3; i++)
{
scanf("%d", &parray[j][i]);
}
}
return 0;
}
int main(void)
{
int a[2][3] = {0};
InputArray(a, 2);
OutputArray(a, 2);
return 0;
}
6.二级指针:指向指针变量的指针
1.函数体内想修改函数体外指针变量值的时候,传指针变量的地址即二级指针
2.指针数组的数组名是指向数组第一个元素的指针,即指向指针的指针也就是二级指针
#include <stdio.h>
int main(void)
{
int a = 100;
int *p = NULL;
int **q = NULL;
p = &a;
q = &p;
printf("=====================\n");
printf("&a:%p\n", &a);
printf("&p:%p\n", &p);
printf("&q:%p\n", &q);
printf("=====================\n");
printf("a:%d\n", a);
printf("p:%p\n", p);
printf("q:%p\n", q);
printf("=====================\n");
printf("*p:%d\n", *p);
printf("*q:%p\n", *q);
printf("=====================\n");
printf("**q:%d\n", **q);
return 0;
}
#include <stdio.h>
int fun(char **pptmp)
{
*pptmp = "hello world";
return 0;
}
int main(void)
{
char *p = NULL;
fun(&p);
printf("p = %s\n", p);
return 0;
}
二级指针:1.函数体内想修改外部指针变量值传入二级指针
2.指针数组的数组名是二级指针