1

When I am trying to pass two dimensional array as a parameter In C,

void PrintTriangle(int *triangle)

It's doesn't works for me why ?

4
  • How you called printTriangle? Posting your code/snippet may help us to answer this question. Commented Sep 20, 2013 at 17:28
  • What isn't working? Please provide details: how you passed in the array, what you tried to do in printTriangle and how it failed. Commented Sep 20, 2013 at 17:29
  • @haccks int pascalTriangle[5][5]; pascalTriangle[0][0] = 1; PrintTriangle(pascalTriangle); Commented Sep 20, 2013 at 17:29
  • 1
    Read: c++ dynamic array initalization with declaration Commented Sep 20, 2013 at 17:32

3 Answers 3

1

You must specify the dimension of the outermost array when passing multidimensional arrays as parameters

Something like this:-

void PrintTriangle(int pascalTriangle[][5]);

Calling PrintTriangle(pascalTriangle); like this is wrong as your function PrintTriangle() is expecting a pointer to integer.

Sign up to request clarification or add additional context in comments.

4 Comments

so how do you specify the dimension ?
@haccks:- The reason why it is wrong is very similar to what you mentioned. Still updated that in my answer!!! Thanx!! :)
Also read the comments to this answer.
The last dimension is for the innermost array, not outermost. In int a[2][3], a[i] is an aggregate object that is an array of three int. So arrays corresponding to the last dimension are inside the arrays of previous dimensions.
1

Here's how I memorized passing multidemensional arrays as parameters. This might be a little childish but works for me.
Suppose You have an array defined

int numbers[3][4];

and you want to pass it to function.
To do it correctly I answer series of questions:
1) What do I want to pass to function? An array.(in this case numbers).
2) What's numbers? Numbers is an address of its first element(without going into details);
3)What's the first element of numbers?(in this case it's an array of 4 integers)
4) So what I want as function formal parameter is a pointer to answer from step 3:

int  (*pointer)[4]

It might be slow and looks unprofessional but you can ask the same question on array of every dimension you can think of. It's also useful to ask yourself that when not passing whole array but one of subarrays.

Hope it helps you as it helped me.

2 Comments

You can simple declare the parameter the same way the array is declared: void foo(int numbers[3][4]);. The compiler will automatically adjust it to int (*numbers)[4].
@EricPostpischil as I mentioned, it's not fast nor smart, just something that I used when learned about multidimensional arrays myself.Helped me to understand pointers and arrays of any size better. Also, it leaves almost no place for silly mistakes.
1

Calling your function as

 PrintTriangle(pascalTriangle); 

is wrong. Because PrintTriangle() expecting a pointer to integer as its argument and pascalTriangle (after decaying) is of type int(*)[5].Also you must have to pass the number of rows and columns (as arguments) in array pascalTriangle. Prototype for this function should be

void PrintTriangle(int row, int col, int *triangle);

and a call to it may be

PrintTriangle(5, 5, &pascalTriangle[0][0]); 

5 Comments

@H2CO3; Ya. I know, and you know what that mean in fact :)
I only encourage you to use the correct wording, which now you do :)
You're welcome, don't take it as nitpicking when I'm correcting something minor in what you wrote. I often do this to people only in order to save them from potential mass-downvoting :P and for the sake of learning, in first place.
C does not require you to pass the number of rows and columns of a two-dimensional array, just the number of columns. (The called function might require it for other reasons.)
@EricPostpischil; Read comments to this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.