gpt4 book ai didi

c - 表达式必须有一个指向 C 中对象类型的指针

转载 作者:行者123 更新时间:2023-12-04 09:51:02 26 4
gpt4 key购买 nike

我试图在函数和数组中使用指针,当我在 main 中调用报告函数时,我不断收到错误表达式必须有一个指向对象类型的指针。我已经尝试了一切。似乎没有任何工作。谁能让我知道我做错了什么?

请注意:没有 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);
}

最佳答案

在报告功能中删除该行并查看以下报告功能:

int row = 0, column = 0;

在函数中,使用列表作为
int list[][5]

通话 list 为
list

不像
list[0][0]

这是完整的代码:
#include <stdio.h>
#include <stdlib.h>

void print(int list[][5], 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]);
printf("\n");
}
}

void rowaverage(int list[][5], 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++;
}
printf("Average of row %d is %.2f\n", row, (sum / count));
}
}

void allaverage(int list[][5], 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++;
}
}

printf("Average of all elements in array is %.2f\n", (sum / count));
}

void largest(int list[][5], int row_count, int column_count)
{
int column = 0, row = 0;
int largest = list[0][0];

for (row = 0; row < row_count; row++) {
for (column = 0; column < column_count; column++) {
if (largest < list[row][column]) {
largest = list[row][column];
}
}
}

printf("The largest number in the array is %d\n", largest);
}

void report(int list[][5], int row_count, int column_count)
{
print(list, row_count, column_count);
printf("\n");
rowaverage(list, row_count, column_count);
printf("\n");
allaverage(list, row_count, column_count);
printf("\n");
largest(list, row_count, 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, row, column);

return 0;
}

关于c - 表达式必须有一个指向 C 中对象类型的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20064650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com