[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Multidimensional arrays in C

> how can i pass a pointer to a multi-dimensional array to a function.all i'm
> able to pass,is a pointer to a single dimension of the array.i want to pass
> the entire array.

One thing cool about C(and C++) is that a part of an array can be treated
as an array.
it's perfectly fine to declare a variable like this int t[10][10]
and pass it like so function(t) where function is of type function(int
However, some compilers differentiate between a pointer to a
pointer (int **)and an array of arrays(int a[.][.]) and produces a type
incompatibility message which can be remedied by declaring your variable
as int ** and then dynamically allocating the space you need.
And as for modifying contents of arrays, there are lots of subtle issues
that I can sum up is one statement."All arguments in C are passed by
value". ie. If you pass a pointer to a value. The pointer itself is passed
by value and so if you modify what it points to (especially if the
original paramenter is a pointer itself), the contents of the original
parameter don't change :-}
Do let me know if you want to know more about this. I learnt all this the
hard way(SIGSEGV) and it would be nice if I could prevent this from
happening to someone else. 

Noufal (from LiNaC)

You will always find something in the last place you look.