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 ?

haccks
  • 104,019
  • 25
  • 176
  • 264
TheCrackNuts
  • 47
  • 1
  • 6

3 Answers3

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.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • so how do you specify the dimension ? – TheCrackNuts Sep 20 '13 at 17:30
  • 1
    @haccks:- The reason why it is wrong is very similar to what you mentioned. Still updated that in my answer!!! Thanx!! :) – Rahul Tripathi Sep 20 '13 at 17:45
  • Also read the comments to this [answer](http://stackoverflow.com/a/18661858/2455888). – haccks Sep 20 '13 at 18:06
  • 2
    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. – Eric Postpischil Sep 20 '13 at 18:51
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]); 
Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • @H2CO3; Ya. I know, and you know what that mean in fact :) – haccks Sep 20 '13 at 17:48
  • 1
    I only encourage you to use the correct wording, which now you do :) –  Sep 20 '13 at 17:48
  • 1
    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. –  Sep 20 '13 at 17:58
  • 1
    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.) – Eric Postpischil Sep 20 '13 at 18:55
  • @EricPostpischil; Read comments to this [answer](http://stackoverflow.com/a/18661858/2455888). – haccks Sep 20 '13 at 18:58
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.

zubergu
  • 3,646
  • 3
  • 25
  • 38
  • 1
    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]`. – Eric Postpischil Sep 20 '13 at 18:58
  • @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. – zubergu Sep 20 '13 at 19:01