1. ホーム
  2. c

[解決済み] 式はCのオブジェクト型へのポインタを持たなければならない

2022-02-14 16:18:45

質問

関数や配列でポインターを使おうとしているのですが、mainでreport関数を呼び出すと、Expression must have a pointer to object typeというエラーがずっと出ます。いろいろと試してみました。何もかも試しましたが、うまくいかないようです。どなたか、私が何を間違えているのか、教えていただけませんか?

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[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;
}