Home > Software design >  expected ‘int **’ but argument is of type ‘int (*)[3]’
expected ‘int **’ but argument is of type ‘int (*)[3]’

Time:10-27

I just started to tinker with pointers in multidimensional arrays and was trying to pass and array into a void function. The compilator just threw an error. I need the array to be passed exactly as a pointer to maybe change it then by reference.

#include <stdio.h>
#include <stdlib.h>

void Func(int** matrix, int sizeFirst, int sizeSecond)
{
    for (int i = 0; i < sizeFirst; i  )
    {
        for (int j = 0; j < sizeSecond; j  )
        {
            printf("%d\n", matrix[i][j]);
        }
    }
}

int main()
{

    int array[2][3] = {
        {5,8,2},
        {1,3,6}
    };

    int sizeMain = sizeof(array)/sizeof(array[0]);
    int sizeInner = sizeof(array[0])/sizeof(array[0][0]);
 
    Func(array, sizeMain, sizeInner);

    return 0;
}

CodePudding user response:

2D array is not pointer to pointer. Your code is invalid as Func does not know how many columns every row has. Also, use the correct type for sizes & indexes

void Func(size_t sizeFirst, size_t sizeSecond, int (*matrix)[sizeSecond])
{
    for (int i = 0; i < sizeFirst; i  )
    {
        for (int j = 0; j < sizeSecond; j  )
        {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main()
{

    int array[2][3] = {
        {5,8,2},
        {1,3,6}
    };

    size_t sizeMain = sizeof(array)/sizeof(array[0]);
    size_t sizeInner = sizeof(array[0])/sizeof(array[0][0]);
 
    Func(sizeMain, sizeInner, array);

    return 0;
}

https://godbolt.org/z/Ejfrdd3nK

CodePudding user response:

First of you have to understand what int ** represent, it represents pointer to integer pointer, here's an example:

int a = 10;
int *p = &a; // 'p' points to 'a`
int **pp = &p; // 'pp' points to 'p' (pointer to integer)

So because of this, you can't use int ** for an integer array. Now that's out of the way, let's see what can you do instead (solution)

You can simply add a pointer to an integer array in a function definition like this

void Func(int *matrix[], int rows, int cols)

Or you can simply do

void Func(int matrix[rows][cols])

Note: Array of strings or 2D character array (char array[][]) can be represented as char**

CodePudding user response:

With pointers to VLAs, you can preserve all of the dimensions in the type of the argument and then let C do the multidimensional scaling for you.

In older versions of C you need to calculate the spot manually, remembering that the first dimension from the right moves by 1 (scaled by sizeof(type), which C does for you), the second by first dimension from the right, the third by first dimension from the right multiplied by second dimension from the right and so on.

It's sort of like digits in numbers (units, tens, hundreds, ...) except that the next scaling is the previous scaling multiplied by the previous dimension rather than by a constant radix. (You could let C convert groups of decimal digits to decimal numbers by letting it subtract some &decdigits[a][b][c] and &decdigits[0][0][0] where decdigits is some decdigits[10][10][10], i.e.: char decdigits[10][10][10]; printf("%d\n", (int)(&decdigits[1][2][3] - &decdigits[0][0][0])) /*prints 123*/;)

#include <stdio.h>

void Func( int sizeFirst, int sizeSecond, int (*matrix)[sizeFirst][sizeSecond])
{
    for (int i = 0; i < sizeof((*matrix))/sizeof((*matrix)[0]) ; i  )
    {
        for (int j = 0; j < sizeof((*matrix)[0])/sizeof((*matrix)[0][0]) ; j  )
        {
            printf("%d ", (*matrix)[i][j]);
        }
        puts("");
    }
}

void Func_89( int sizeFirst, int sizeSecond, int *matrix)
{
    for (int i = 0; i < sizeFirst; i  )
    {
        for (int j = 0; j < sizeSecond; j  )
        {
            printf("%d ", matrix[i*sizeSecond   j]);
        }
        puts("");
    }
}


int main()
{

    int array[2][3] = {
        {5,8,2},
        {1,3,6}
    };

    int sizeMain = sizeof(array)/sizeof(array[0]);
    int sizeInner = sizeof(array[0])/sizeof(array[0][0]);

    Func(sizeMain, sizeInner,&array );
    puts("===");
    Func_89(sizeMain, sizeInner,&array[0][0] );

    //char decdigits[10][10][10]; printf("%d\n", (int)(&decdigits[1][2][3] - &decdigits[0][0][0])); //123
    
    return 0;
}
  • Related