1)Can you suggest me how to assign a pre-defined 2-dimensional array to a pointer.The intension here is to pass 2-D array to a funtion so that changes in the function will reflect the 2-D array .

void main(){   
 int array[40][40];
 fun(array);
}

void fun(int **array){    ........}

The above code is giving error as suspecious pointer conversion by the parameter passed, int ** array, in function fun()Pls suggest me the right function parameter along with the usage to access any two numbers ie accessing say array[0][0],array[0][1] in the fuction fun().Waiting for ur answers Yours Raju

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

int func();

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");
	func(mat);
	return 0;
}

int func(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;
}

Code was taken from this
P.S Instead of void main use int main

> void fun(int **array)
The rule about arrays become pointers is not recursive - it only happens for the outermost level of the array. void fun ( int (*array)[40] ); is one way to declare the function.

This is another way, which is easier to remember since it's just copy/paste of the original array that you want to pass in the first place. void fun ( int array[40][40] ); Either form is equivalent, and you can of course use [row][col] indexing on the array inside the function just as you would outside the function.


Yes, main does return an int by the way.

@Andor

Asssuming that the elements in the two dimensional array are contiguous doesnt necessarily hold true. For performace reasons, the data is aligned with the byte boundaries (which in the case of 32 bit computers is 4 bytes). So an optimizing compiler might leave in empty spaces or gaps between the elements of a single row to achieve.

Thus relying on a double pointer to be a two dimensional array is not always true (Like MR. Salem has already explained).

Something like teh below code will be much safer.

int func([B]short mat [][3][/B])
        {
        short    i, j, *index[3];

        for (i = 0; i < 3 ; i++)
                [B]index[i] = &mat [i][0] ;[/B]

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

Hope it helped, bye.

This question has already been answered. Start a new discussion instead.