Saturday, May 22, 2010

Plzz help me out! How to pass a 2-D array from main() to other function in C, using pass by value.....?

i already posted this Q.. but i didnt get the answer.. please reply to da answer wid an appropriate example.. thnq..

Plzz help me out! How to pass a 2-D array from main() to other function in C, using pass by value.....?
All arrays in C are passed by reference (i.e. pointer). Note that if you pass the whole array you don't need an address operator to pass the array because the name of the array already equals the address of the first array element.





Another thing to keep in mind is that you almost always need to pass the dimensions of the array to the called function or (unless it never changes) it won't know exactly what size it is dealing with. This is true of arrays of any dimension.








#include %26lt;stdio.h%26gt;





void func(int a[][10], int row, int col)


{


int i, j, k;





for (i = 0, k = 0; i %26lt; row; i++)


{


for (j = 0; j %26lt; col; j++)


{


a[i][j] = k++;


}


}


}








int main()


{


const int ROW = 5;


const int COL = 10;





int a[ROW][COL];





func(a, ROW, COL);





int i, j;





for (i = 0; i %26lt; ROW; i++)


{


for (j = 0; j %26lt; COL; j++)


{


printf("%2d ", a[i][j]);


}





printf("\n");


}


}
Reply:Listen to haunted_divinity for he speaks the truth.
Reply:U cant pass a 2d array by pass by value.


it has to be done by pointers.


main()


{


int a[100];


...


func(a);


...


}


func(int a[])


{


...


}


wut u are doing here is passing the address of the first cell of the array a.
Reply:char a[4][4];


callfunct(a);


No comments:

Post a Comment