5

I am trying to use pointers in functions and arrays and when I call the report function in main, I keep getting an error Expression must have a pointer to object type. I have tried everything. Nothing seem to be working. Can anyone please let me know what I am doing wrong?

Please note: without the report function, if I call the other functions separately in main it works. It's not working only with the report function.

#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);
}
alk
  • 69,737
  • 10
  • 105
  • 255
anansharm
  • 183
  • 2
  • 4
  • 13

4 Answers4

9

In report function remove that line and see below report function:

int row = 0, column = 0;

In functions, use list as

int list[][5]

Call list as

list

not as

list[0][0]

Here is the complete code:

#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;
}
taneryilmaz
  • 442
  • 5
  • 10
  • How will i solve this with pointers and arrays?? i actually wanted to solve it with pointers on functions and arrays. – anansharm Nov 19 '13 at 07:03
  • You are using static array.So you must provide array width on functions. For dynamic allocated arrays, you can use pointer (as int **list) for functions. – taneryilmaz Nov 19 '13 at 07:22
  • 1
    You can also follow those links: - http://c-faq.com/aryptr/pass2dary.html - http://stackoverflow.com/questions/8767166/passing-2d-array-to-function – taneryilmaz Nov 19 '13 at 07:23
0

So you're a passing a pointer to int in report function:

int list[3][5];
report(&list[0][0], row, column);
...
void report(int *list, int row_count, int column_count)
...

But then you are using list in report function as pointer to pointer to int:

list[0][0]

But it is not a pointer to pointer to int. It just has a type of int*. So "list" is dereferenced twicely in report functions. That's wrong.

You can fix it if you use just "list" as parameter in function calls in report() without "[0][0]":

rowaverage(list, row, column);
Michael
  • 1,505
  • 14
  • 26
  • @RolandIllig Pointer to the first element of matrix is passed to report(). Then it is dereferenced in it twicely in original. It is a crash. But if it is dereferenced only once it will be the first element of the matrix. Why it wouldn't work? – Michael Nov 19 '13 at 06:45
  • @RolandIllig I see what you are talking. Fixed. – Michael Nov 19 '13 at 06:52
0

Maybe you can change the parameter list[0][0] to list. As list is a pointer to int, but list[0][0] is just an int. Their type is different, and the first parameter in your functions, a pointer is needed. :)

0

You are doing your arrays totally wrong everywhere.

Forget & and forget * for the time being. You don't need any in this program.

Your list is an int[3][5], not an int* and not anything else. Declare it as such in all functions.

void rowaverage(int list[3][5], ...

and so on. Access it in the function like this:

list[row][column]

not list+row*column_count+column

Pass the list to functions like this:

rowaverage(list, ...

not rowaverage(&list[0][0], ....,

I have no idea why you wrote report differently from all the other functions, but it doesn't matter because all the other functions are totally wrong too. If they are wirking, it's by sheer luck.

Pick up a good C book and read it.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • i wanted to use pointers in functions and 2D arrays, i wanted to know how will i be able to solve it with pointers and arrays. – anansharm Nov 19 '13 at 07:02
  • It's OK to write unusually complicated and/or suboptimal code as an exercise, but you should warn us about that beforehand so that we don't waste time trying to correct it. – n. m. could be an AI Nov 19 '13 at 07:14