首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >表达式必须有指向C中对象类型的指针。

表达式必须有指向C中对象类型的指针。
EN

Stack Overflow用户
提问于 2013-11-19 06:26:30
回答 4查看 75.6K关注 0票数 5

我试图在函数和数组中使用指针,当我在main中调用report函数时,我一直得到一个错误表达式,必须有一个指向对象类型的指针。我什么都试过了。似乎什么都没起作用。有人能告诉我我做错了什么吗?

请注意:如果没有report函数,如果我在main中单独调用其他函数,它就能工作。它不仅适用于report函数。

代码语言:javascript
复制
#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);
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-11-19 06:48:19

在report函数中,删除该行并参见下面的report函数:

代码语言:javascript
复制
int row = 0, column = 0;

在函数中,使用列表作为

代码语言:javascript
复制
int list[][5]

呼叫列表

代码语言:javascript
复制
list

不像

代码语言:javascript
复制
list[0][0]

以下是完整的代码:

代码语言:javascript
复制
#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;
}
票数 9
EN

Stack Overflow用户

发布于 2013-11-19 06:35:22

因此,在report函数中,您是一个指向int的指针:

代码语言:javascript
复制
int list[3][5];
report(&list[0][0], row, column);
...
void report(int *list, int row_count, int column_count)
...

但是,您要使用report函数中的list作为指针指向int:

代码语言:javascript
复制
list[0][0]

但它不是指向int的指针。它只是有一种int*类型。因此,"list“在报表函数中被取消两次引用。这是错误的。

如果在报告()中使用"list“作为函数调用中的参数,而不使用”:

代码语言:javascript
复制
rowaverage(list, row, column);
票数 0
EN

Stack Overflow用户

发布于 2013-11-19 06:40:45

也许您可以将参数list更改为list。因为list是int的指针,而list只是int的指针。它们的类型不同,函数中的第一个参数需要一个指针。:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20064650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档