c语言中多维数组的指针表示
学c的时候碰见了下面这道题
修改下面的程序,让它从数组计算变成指针计算:
/* rain.c -- finds yearly totals, yearly average, and monthly
average for several years of rainfall data */
#include <stdio.h>
#define MONTHS 12 // number of months in a year
#define YEARS 5 // number of years of data
int main(void)
{
// initializing rainfall data for 2010 - 2014
const float rain[YEARS][MONTHS] =
{
{4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
{8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
{9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
{7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
{7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}
};
int year, month;
float subtot, total;
printf(" YEAR RAINFALL (inches)\n");
for (year = 0, total = 0; year < YEARS; year++)
{ // for each year, sum rainfall for each month
for (month = 0, subtot = 0; month < MONTHS; month++)
subtot += rain[year][month];
printf("%5d %15.1f\n", 2010 + year, subtot);
total += subtot; // total for all years
}
printf("\nThe yearly average is %.1f inches.\n\n",
total/YEARS);
printf("MONTHLY AVERAGES:\n\n");
printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
printf(" Nov Dec\n");
for (month = 0; month < MONTHS; month++)
{ // for each month, sum rainfall over years
for (year = 0, subtot =0; year < YEARS; year++)
subtot += rain[year][month];
printf("%4.1f ", subtot/YEARS);
}
printf("\n");
return 0;
}
实则就是考验数组和指针的关系,即从数组表示变成指针表示。为了新建了一个测试文件:
#include <stdio.h>
#define Size 5
int print_all_arr(const float test[Size][Size]);
int print_test(const float *test,char test_s[]);
int main(void)
{
const float test[Size][Size] =
{
{0.0, 0.1, 0.2, 0.3, 0.4},
{1.0, 1.1, 1.2, 1.3, 1.4},
{2.0, 2.1, 2.2, 2.3, 2.4},
{3.0, 3.1, 3.2, 3.3, 3.4},
{4.0, 4.1, 4.2, 4.3, 4.4}};
print_all_arr(test);
printf("开始数组表示:\n");
print_test(&test[1][2],"test[1][2]");
print_test(test[1],"test[1]");
print_test(test,"test");
printf("开始指针表示:\n");
print_test(&test,"test");
print_test(&*(test+1),"*(test+1)");
print_test(&*(*(test + 1) + 2),"*(*(test + 1) + 2)");
return 0;
}
int print_all_arr(const float test[Size][Size])
{
for (int i = 0; i < Size; i++)
{
for (int n = 0; n < Size; n++)
{
printf("%f ", test[i][n]);
// printf("%i %i ", i, n);
printf("%p | ", &test[i][n]);
}
printf("\n");
}
}
int print_test(const float *test,char test_s[])
{
printf("它的值为:%f 地址为%p 使用表达式:%s\n", *test, test,test_s);
}
此程序输出为:
0.000000 0x7ffe53ddb480 | 0.100000 0x7ffe53ddb484 | 0.200000 0x7ffe53ddb488 | 0.300000 0x7ffe53ddb48c | 0.400000 0x7ffe53ddb490 |
1.000000 0x7ffe53ddb494 | 1.100000 0x7ffe53ddb498 | 1.200000 0x7ffe53ddb49c | 1.300000 0x7ffe53ddb4a0 | 1.400000 0x7ffe53ddb4a4 |
2.000000 0x7ffe53ddb4a8 | 2.100000 0x7ffe53ddb4ac | 2.200000 0x7ffe53ddb4b0 | 2.300000 0x7ffe53ddb4b4 | 2.400000 0x7ffe53ddb4b8 |
3.000000 0x7ffe53ddb4bc | 3.100000 0x7ffe53ddb4c0 | 3.200000 0x7ffe53ddb4c4 | 3.300000 0x7ffe53ddb4c8 | 3.400000 0x7ffe53ddb4cc |
4.000000 0x7ffe53ddb4d0 | 4.100000 0x7ffe53ddb4d4 | 4.200000 0x7ffe53ddb4d8 | 4.300000 0x7ffe53ddb4dc | 4.400000 0x7ffe53ddb4e0 |
开始数组表示:
它的值为:1.200000 地址为0x7ffe53ddb49c 使用表达式:test[1][2]
它的值为:1.000000 地址为0x7ffe53ddb494 使用表达式:test[1]
它的值为:0.000000 地址为0x7ffe53ddb480 使用表达式:test
开始指针表示:
它的值为:0.000000 地址为0x7ffe53ddb480 使用表达式:test
它的值为:1.000000 地址为0x7ffe53ddb494 使用表达式:*(test+1)
它的值为:1.200000 地址为0x7ffe53ddb49c 使用表达式:*(*(test + 1) + 2)
也就是说:
test[1]与*(test+1)等价
test[1][2]与((test+1)+2)等价
那么可知该题把rain[year][month]
改为*(*(rain+year)+month)
即可
ps:但是谁会用指针写法啊啊啊啊,这也太麻烦了啊啊啊啊
标签:year,print,month,test,数组,printf,多维,Size,指针 From: https://www.cnblogs.com/Lemon-233/p/16853542.html