If you're struggling, duplicate your declaration:
void foo ( int *arg[2][3] );
int main ( void )
{
int *ptr[2][3];
foo ( ptr );
return 0;
}
Within foo, you use arg just as you would use ptr unless you're trying to use sizeof, in which case you probably won't get the result you want.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>So in the function declaration/definition I can not specify the rows and columns size.
Then you can't use an array. The size of the first dimension may be omitted, but the others are required, and the size of an array dimension must be a compile-time constant. If you want to pass a two dimensional array of any size to a function, you use dynamic memory:
#include <stdlib.h>
void foo ( int ***arg );
int main ( void )
{
int ***ptr = malloc ( 2 * sizeof *ptr );
int i;
for ( i = 0; i < 2; i++ )
ptr[i] = malloc ( 3 * sizeof *ptr[i] );
foo ( ptr );
for ( i = 0; i < 2; i++ )
free ( ptr[i] );
free ( ptr );
return 0;
}
You have more options in C++, but you neglected to mention what language you're using.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
here is how you initialize the function and pass the values
char *function(char **array)
{
//your code here
return 0;
}
int main()
{
//put values in array
function(p); //passing to function
}
arshad115
Junior Poster in Training
65 posts since Nov 2008
Reputation Points: 7
Solved Threads: 2
Maybe you want to take a look at my code:
#include <iostream>
using namespace std;
void func(char **& ref_to_ptr); /* Function declaration */
int main(void)
{
/* Declare the '2D Array' */
char ** ptr = new char * [5];
ptr[3] = new char[20];
/* Put some data in the array */
ptr[3] = "k";
/* Print the first value on the screen */
cout << "First value: " << ptr[3] << endl;
/* Pass the array by reference to the function 'func()' */
func(ptr);
/* Again we print on the screen what's in the '2D Array' */
cout << "Second value: " << ptr[3] << endl;
/* Wait for the user to press ENTER */
cin.get();
/* Cleanup */
delete[] ptr;
/* Tell the Operating System that everything went well */
return 0;
}
void func(char **& ref_to_ptr)
{
/* This function demonstrates how to change the value of it's argument(s) */
ref_to_ptr[3] = "tux4life";
}
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243