Clark Gaebel
Clark Gaebel

Reputation: 17968

Passing a multidimensional array

I know that for 1-dimensional arrays, I can do...

void g(int x[]) {}

void f(int a, int b)
{
    int x[a];
    g(x);
}

But with code such as...

void f(int a, int b)
{
    int x[a][b][4];
    g(x);
}

What would the type signature of g(x) look like?

Upvotes: 3

Views: 175

Answers (2)

Prasoon Saurav
Prasoon Saurav

Reputation: 92874

void g(int x[][b][4]) // b must be known in advance
{}

Otherwise explicitly pass b

For example:

void g(int b,int x[][b][4]){ 

} 

int main() 
{ 
    int a=4,b=6; 
    int x[a][b][4]; 
    g(b,x); 
    return 0; 
}

Upvotes: 5

Brian Clements
Brian Clements

Reputation: 3905

You need to specify the sizes of the arrays:

void g(int x[][2][3]){ 
    /* stuff */
} 

int main() 
{ 
    int x[1][2][3]; 
    g(x); 
    return 0; 
}

Upvotes: 0

Related Questions