0

Possible Duplicate:
Create a pointer to two-dimensional array

When I calling the functions func4() and func5() i get the followinfg errors:

func4() error: cannot convert ‘short int (*)[3]’ to ‘short int**’ for argument ‘1’ to ‘int func4(short int**)’| func5() error: cannot convert ‘short int (*)[3]’ to ‘short int**’ for argument ‘1’ to ‘int func5(short int**)’|

How can I correct the error on calling functions func4() and func5()? Here is my code:

#include <cstdio>

int func1(short mat[][3]);
int func2(short (*mat)[3]);
int func3(short *mat);
int func4(short **mat);
int func5(short *mat[3]);

int main()
{

short mat[3][3],i,j;

for(i = 0 ; i < 3 ; i++)
    for(j = 0 ; j < 3 ; j++)
    {
        mat[i][j] = i*10 + j;
    }

printf(" Initialized data to: ");
for(i = 0 ; i < 3 ; i++)
{
    printf("\n");
    for(j = 0 ; j < 3 ; j++)
    {
        printf("%5.2d", mat[i][j]);
    }
}

printf("\n");

func1(mat);
func2(mat);
func3(&mat[0][0]);
func4(mat); //error: cannot convert ‘short int (*)[3]’ to 
            //‘short int**’ for argument ‘1’ to       ‘int func4(short int**)’|
func5(mat); //error: cannot convert ‘short int (*)[3]’ to 
            //‘short int**’ for argument ‘1’ to ‘int func5(short int**)’|

return 0;
}



/*
Method #1 (No tricks, just an array with empty first dimension)
===============================================================
You don't have to specify the first dimension!
*/

int func1(short mat[][3])
{
register short i, j;

printf(" Declare as matrix, explicitly specify second dimension: ");
for(i = 0 ; i < 3 ; i++)
{
    printf("\n");
    for(j = 0 ; j < 3 ; j++)
    {
        printf("%5.2d", mat[i][j]);
    }
}
printf("\n");

return 0;
}

/*
Method #2 (pointer to array, second dimension is explicitly specified)
======================================================================
*/

int func2(short (*mat)[3])
{
register short i, j;

printf(" Declare as pointer to column, explicitly specify 2nd dim: ");
for(i = 0 ; i < 3 ; i++)
{
    printf("\n");
    for(j = 0 ; j < 3 ; j++)
    {
        printf("%5.2d", mat[i][j]);
    }
}
printf("\n");

return 0;
}

/*
Method #3 (Using a single pointer, the array is "flattened")
============================================================
With this method you can create general-purpose routines.
The dimensions doesn't appear in any declaration, so you
can add them to the formal argument list.

The manual array indexing will probably slow down execution.
*/

int func3(short *mat)
{
register short i, j;

printf(" Declare as single-pointer, manual offset computation: ");
for(i = 0 ; i < 3 ; i++)
{
    printf("\n");
    for(j = 0 ; j < 3 ; j++)
    {
        printf("%5.2d", *(mat + 3*i + j));
    }
}
printf("\n");

return 0;
}

/*
Method #4 (double pointer, using an auxiliary array of pointers)
================================================================
With this method you can create general-purpose routines,
if you allocate "index" at run-time.

Add the dimensions to the formal argument list.
*/

int func4(short **mat)
{
short    i, j, *index[3];

for (i = 0 ; i < 3 ; i++)
    index[i] = (short *)mat + 3*i;

printf(" Declare as double-pointer, use auxiliary pointer array: ");
for(i = 0 ; i < 3 ; i++)
{
    printf("\n");
    for(j = 0 ; j < 3 ; j++)
    {
        printf("%5.2d", index[i][j]);
    }
}
printf("\n");

return 0;
}

/*
Method #5 (single pointer, using an auxiliary array of pointers)
================================================================
*/

int func5(short *mat[3])
{
short i, j, *index[3];
for (i = 0 ; i < 3 ; i++)
    index[i] = (short *)mat + 3*i;

printf(" Declare as single-pointer, use auxiliary pointer array: ");
for(i = 0 ; i < 3 ; i++)
{
    printf("\n");
    for(j = 0 ; j < 3 ; j++)
    {
        printf("%5.2d", index[i][j]);
    }
}
printf("\n");
return 0;
}
Community
  • 1
  • 1
Otvos Barna
  • 81
  • 1
  • 4
  • I have a suggestion for you: do not write multi-language source files. For example: the header `` is not a C header; the `printf()` way of writing is usual in C (maybe `cout` is more usual in C++). – pmg Dec 31 '12 at 10:22

3 Answers3

1

You can't send a two dimensional array to function without specifying the length or size of second dimension of the array. This is what causing the error.

Try this:

int func4(short mat[][3])
{
short    i, j, *index[3];

for (i = 0 ; i < 3 ; i++)
index[i] = (short *)mat + 3*i;

printf(" Declare as double-pointer, use auxiliary pointer array: ");
for(i = 0 ; i < 3 ; i++)
{
printf("\n");
for(j = 0 ; j < 3 ; j++)
{
    printf("%5.2d", index[i][j]);
}
}
printf("\n");

return 0;
}


int func5(short mat[][3])
{
short i, j, *index[3];
for (i = 0 ; i < 3 ; i++)
index[i] = (short *)mat + 3*i;

printf(" Declare as single-pointer, use auxiliary pointer array: ");
for(i = 0 ; i < 3 ; i++)
{
printf("\n");
for(j = 0 ; j < 3 ; j++)
{
    printf("%5.2d", index[i][j]);
}
}
printf("\n");
return 0;

}

Remember this that if something can be done easily and cleanly through one way, don't try to do it through a dirty and difficult way as it will confuse yourself when you are revising or updating your code in the future.

Alfred
  • 1,543
  • 7
  • 33
  • 45
1

1.They are the same:

int func(short **mat);
int func(short *mat[]);
int func(short *mat[3]);

And short ** is not compatible with short (*)[3], because the types they point to are different. short ** points to short * while short (*)[3] points to short[3].

2.Maybe you can try this:

int funcMy(void *mat)   // OK! -added by Justme0 2012/12/31
{
    short i, j, *index[3];
    for (i = 0 ; i < 3 ; i++)
        index[i] = (short *)mat + 3*i;

    printf(" Declare as (void *) pointer, use auxiliary pointer array: ");
    for(i = 0 ; i < 3 ; i++)
    {
        printf("\n");
        for(j = 0 ; j < 3 ; j++)
        {
            printf("%5.2d", index[i][j]);
        }
    }
    printf("\n");
    return 0;
}

3.I think the 3rd func is the best! It uses a trick named "flattening the array" that I read in POINTERS ON C.

Method #3 (Using a single pointer, the array is "flattened") ============================================================ With this method you can create general-purpose routines. The dimensions doesn't appear in any declaration, so you can add them to the formal argument list.

The manual array indexing will probably slow down execution.

Justme0
  • 125
  • 1
  • 12
0

The problem is in incorrect defining of the matrix for func4() and func5().

You define it as short mat[3][3], but in this case you actually do not allocate an array of pointers to matrix lines. You are getting a one pointer for one continuous memory block.

If you want pass arguments matrix as short int**, you should to define it as following:

#include <stdlib.h>

short int** mat;

for(int i = 0; i < 3; i++) {
    mat[i] = (short int*)malloc (3*sizeof(short int));
    for(int j = 0; j < 3; i++) {
         mat[i][j] = i*10 + j;
    }
}
Alex
  • 9,891
  • 11
  • 53
  • 87
  • Thank you for your answer. The solution without changing the functions func4() and func5() is calling the functions from main: func4((short **)mat); func5((short **)mat); – Otvos Barna Dec 31 '12 at 11:03