C / C++ FAQs & Programming Resources - ProkutFAQ : PassingArrayArguments

HomePage Recent Changes Recently Commented Login/Register

Revision [855]

Most recent edit made on 2009-08-08 18:22:46 by SharathAV

Additions:
Since the function is accessing the array using pointer, when you make modification to the array values in myFunction, it makes modification in the original array in main function. If you don't want this to happen, the other way around is to wrap the array in a structure, and pass the structure object to function.


Deletions:
Since the function is accessing the array using pointer, when you make modification to the array values, it makes modification in the original array in main function. If you don't want this to happen, you the other way around is to wrap the array in a structure, and pass the structure object to function.




Revision [854]

Edited on 2009-08-08 18:21:30 by SharathAV

Additions:
Note:
Since the function is accessing the array using pointer, when you make modification to the array values, it makes modification in the original array in main function. If you don't want this to happen, you the other way around is to wrap the array in a structure, and pass the structure object to function.




Revision [853]

Edited on 2009-08-08 18:17:05 by SharathAV

Additions:
void myFunction(int(*)[4][4]);




Revision [746]

Edited on 2008-10-21 11:03:54 by SharathAV

Additions:

CategoryPointers




Revision [669]

The oldest known version of this page was edited on 2008-01-16 20:37:07 by SharathAV

How can we pass 2 or 3 dimensional array as argument to function?


You could use pointer to array for that purpose.


Passing 2 dimensional array

Example:
/* This program demonstrates passing a 2 dimensional array of size 4 * 4  */

void myFunction(int(*)[4]);

int main()
{
int a[4][4];
myFunction(a);
return 0;
}

void myFunction(int (*myArray)[4])
{
/* Here you can access the content of the argument passed like any other 2-D array */
/* Example: myArray[2][2]=10; */
}


Passing 3 dimensional array

Example:
/* This program demonstrates passing a 3 dimensional array of size 4 * 4 * 4  */

void myFunction(int(*)[4]);

int main()
{
int a[4][4][4];
myFunction(a);
return 0;
}

void myFunction(int (*myArray)[4][4])
{
/* Here you can access the content of the argument passed like any other 3-D array */
/* Example: myArray[2][2][2]=10; */
}
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki
Page was generated in 0.0396 seconds