我正在尝试在函数和数组中使用指针,但当我在主函数中调用报告函数时,我一直收到错误消息“表达式必须具有对象类型的指针”。我已经尝试了所有方法,但似乎没有起作用。请问我做错了什么吗?
请注意:如果没有“report”函数,如果我在“main”中分别调用其他函数,则可以正常工作。只有在“report”函数中才会出现问题。
请注意:如果没有“report”函数,如果我在“main”中分别调用其他函数,则可以正常工作。只有在“report”函数中才会出现问题。
#include <stdio.h>
#include <conio.h>
void print(int *list, int row_count, int column_count);
void rowaverage(int *list, int row_count, int column_count);
void allaverage(int *list, int row_count, int column_count);
void largest(int *list, int row_count, int column_count);
void report(int *list, int row_count, int column_count);
int main()
{
int i = 1, row, column;
int list[3][5];
printf("Enter 3 sets of 5 integers::\n");
for (row = 0; row < 3; row++)
{
printf("Elements in the %d set are ::\n", row);
for (column = 0; column < 5; column++)
{
printf("Element No. %d is ", i++);
scanf("%d", &list[row][column]);
}
printf("\n");
i = 1;
}
printf("The elements in array are:\n");
report(&list[0][0], row, column);
getch();
return 0;
}
void print(int *list, int row_count, int column_count)
{
int column, row;
for (row = 0; row < row_count; row++)
{
for (column = 0; column < column_count; column++)
{
printf("%8d", *(list + row * column_count + column));
}
printf("\n");
}
}
void rowaverage(int *list, int row_count, int column_count)
{
int column, row;
for (row = 0; row < row_count; row++)
{
float sum = 0, count = 0;
for (column = 0; column < column_count; column++)
{
sum += *(list + row * column_count + column);
count++;
}
printf("Average of row %d is %.2f\n", row, (sum / count));
}
}
void allaverage(int *list, int row_count, int column_count)
{
int column, row;
float sum = 0, count = 0;
for (row = 0; row < row_count; row++)
{
for (column = 0; column < column_count; column++)
{
sum += *(list + row * column_count + column);
count++;
}
}
printf("Average of all elements in array is %.2f\n", (sum / count));
}
void largest(int *list, int row_count, int column_count)
{
int column = 0, row = 0;
int largest = *(list + row * column_count + column);
for (row = 0; row < row_count; row++)
{
for (column = 0; column < column_count; column++)
{
if (largest < *(list + row * column_count + column))
{
largest = *(list + row * column_count + column);
}
}
}
printf("The largest number in the array is %d\n", largest);
}
void report(int *list, int row_count, int column_count)
{
int row = 0, column = 0;
print(list[0][0], row, column);
printf("\n");
rowaverage(list[0][0], row, column);
printf("\n");
allaverage(list[0][0], row, column);
printf("\n");
largest(list[0][0], row, column);
}