昨天学习了指针的用法,在调用函数中使用指针or数组名,可使“值改变”。
今天学习的内容主要是:
用指向数组的指针作函数参数
(1)指向多维数组元素的指针变量
(2)指向由m个元素组成的一维数组的指针变量
区别两者,看下面这个例子:
#include <stdio.h>
int main()
{
void average(float *p, int n);
void search(float(*p)[4], int n);
float score[3][4] = { { 65, 67, 70, 60 }, { 80, 87, 90, 81 }, { 90, 99, 100, 98 } };
average(*score, 12); //列指针,&score[0][0],(1)的应用
search(score, 2); //行指针,&score[0],(2)的应用
return 0;
}
void average(float *p, int n)
{
float *p_end;
float sum = 0, aver;
p_end = p + n - 1;
for (; p <= p_end; p++)
sum = sum + (*p);
aver = sum / n;
printf("average=%5.2f\n", aver);
}
void search(float(*p)[4], int n) //先指向4个元素组成的一维数组,再然后指向数组中的元素
{
int i;
printf("The score of No.%d are:\n", n);
for (i = 0; i < 4; i++)
{
printf("%5.2f", *(*(p + n) + i)); printf("\n");
}
}