954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C Quest 2

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

dsraju
Newbie Poster
6 posts since Sep 2006
Reputation Points: 23
Solved Threads: 0
 
#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

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

> 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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

@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(<strong>short mat [][3]</strong>)
        {
        short    i, j, *index[3];

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

        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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You