Home > front end >  empty pointers as an argument in C - problem with asterisks
empty pointers as an argument in C - problem with asterisks

Time:05-28

The task is to fill an array with numbers by user input and then use our stats() function to calculate average etc. of each row of this array. The header of stats() function looks as follows:

int stats(int (*ptr)[5], int width, int height, int row_id, int* max, int* min, float *avg)

where ptr is a pointer to the matrix,width and height are its size, row_id is the index of analysed row and max, min and avg are pointers to variables storing each statistics.

When calling a function with such line:

stats(*ptr, 5,5,2, *max = NULL, *min = NULL,  *avg=NULL);

the following error appears:

 error: invalid type argument of unary '*' (have 'int')

I tried different approaches but there is always a mistake, how can I fix that? Thank you for any clues.

edit: Here's the whole code:

#include <stdio.h>

int stats(int (*ptr)[5], int width, int height, int row_id, int* max, int* min, float *avg)
{
    int j, vmax,vmin;
    int max = &vmax;
    int min = &vmin;
    int i = row_id;
    int m = *ptr;

    for(j = 0; j<5; j  ){
        if(m[i][j]>max)
        {
            max = m[i][j] ;
            j  ;
            else
                j  ;
        }

    }
printf("%d", max);
    return 0;
}

int main(void){
    int n, i, j, vmin, vmax; // vmax - value of the maximum
    int min = &vmin; // min - pointer to the minimum
    int max = &vmax;
    float vavg;
    int avg = &vavg;
    int m[5][5];

    for(i = 0; i<5; i  )
    {
        for(j = 0; j<5; j  )
        {
            printf("ENTER A NUMBER: ");
            scanf("%d", &n);
            m[i][j] = n;
        }
    }
    int ptr = &m;
    stats(*ptr, 5,5,2, *max = NULL, *min = NULL,  *avg=NULL);

    return 0;
}

CodePudding user response:

Your code full of bugs.

For example min and max are not declared as pointers

int min = &vmin; // min - pointer to the minimum
int max = &vmax;

Also it is unclear why the variable avg has the type int and is initialized by a pointer expression of the type float *.

float vavg;
int avg = &vavg;

Or the variable ptr of the type int is initialized by the address of the two-dimensional array.

int ptr = &m;

As for the function then if the function operates only on one row then there is no any sense to pass to the function the whole two-dimensional array.

Also the return type and the returned value of the function do not make a sense.

And the function shall not output any message. It is the caller of the function that will decide output a message or not.

And also the function contains bugs where you are redeclaring its parameters like for example

int max = &vmax;

that again does not make a sense.

Using your approach the function can be declared and defined the following way

#include <assert.h>

//...

void stats( const int *a, size_t n, int *max, int *min, float *avg )
{
    assert( n != 0 );

    *max = a[0];
    *min = a[0];
    float sum = a[0];

    for( size_t i = 1; i < n; i   )
    {
        sum  = a[i];

        if ( *max < a[i] )
        {
            *max = a[i];
        }
        else if ( a[i] < *min )
        {
            *min = a[i];
        }
    }

    *avg = sum / n;
}

And called like

int min = 0;
int max = 0;
float avg = 0.0f;;

//...

stats( m[2], 5, &max, &min, &avg );

printf( "max = %d\n", max );
printf( "min = %d\n", min );
printf( "average = %f\n", avg );

CodePudding user response:

When you are using pointers as function parameters be carefull. If you have something like this:

int func(int *max){

}

max ,here, is a pointer which needs to hold an address. So inside this function when you need use it, you need to dereference it by using *. For example:

int func(int *max){
   *max = $someAddress
}
  • Related