Passing a two dimensional array to function

S

Sac

Hello C Gurus,

I have following questions regarding passing two dimensional
array to a function:
1. In memory single and two dimensional arrays are stored in
same fashion by allocating a set of consecutive memory locations. Is
there any specific reason for allocating a different type pointer for
two dimensional arrays.
For example:
int a[3][4];
int *p;
p=a;
should works because a internally 'a' points to a set of
consecutive memory locations.

2. As per K&R when 2 dimensional array is passed to a function
number or rows is irrelevant and number of columns is a must. However
when array get passed on to a function its passed as a pointer then
why its required to pass even columns?

Please let me know if questions are not clear.


Thanking in advance
Sac
 
D

Default User

Sac said:
Hello C Gurus,

I have following questions regarding passing two dimensional
array to a function:
1. In memory single and two dimensional arrays are stored in
same fashion by allocating a set of consecutive memory locations. Is
there any specific reason for allocating a different type pointer for
two dimensional arrays.
For example:
int a[3][4];
int *p;
p=a;
should works because a internally 'a' points to a set of
consecutive memory locations.

No, it shouldn't. The variable a doesn't get converted to type int*.
You would have to cast it, and even then it technically could fail.
While not necessary (and not at all common), a compiler could add
bounds checking that would cause runtime failures of some sort.
2. As per K&R when 2 dimensional array is passed to a function
number or rows is irrelevant and number of columns is a must. However
when array get passed on to a function its passed as a pointer then
why its required to pass even columns?

Because of what I said above. You won't have the correct type. When you
pass the variable you declared above, its type is "pointer to array 4
of int". If you don't have that type as your function parameter, it
would require a diagnostic when you pass a. You would need to cast it
and hope that it works (it probably will).

Now, WHY would you want to? The reason for multi-dimensional arrays is
to provide clarity and ease of use. If you aren't going to use the
multi-dimensional nature of a in the function, why ever declare it that
way in the first place? Just make it:

int a[12];


What great problem do you think you're solving with this?



Brian
 
S

Sac

Sac said:
Hello C Gurus,
        I have following questions regarding passing two dimensional
array to a function:
        1. In memory single and two dimensional arrays are stored in
same fashion by allocating a set of consecutive memory locations. Is
there any specific reason for allocating a different type pointer for
two dimensional arrays.
           For example:
           int a[3][4];
           int *p;
           p=a;
          should works because a internally 'a' points to a set of
consecutive memory locations.

No, it shouldn't. The variable a doesn't get converted to type int*.
You would have to cast it, and even then it technically could fail.
While not necessary (and not at all common), a compiler could add
bounds checking that would cause runtime failures of some sort.
   2. As per K&R when 2 dimensional array is passed to a function
number or rows is irrelevant and number of columns is a must. However
when array get passed on to a function its passed as a pointer then
why its required to pass even columns?

Because of what I said above. You won't have the correct type. When you
pass the variable you declared above, its type is "pointer to array 4
of int". If you don't have that type as your function parameter, it
would require a diagnostic when you pass a. You would need to cast it
and hope that it works (it probably will).

Now, WHY would you want to? The reason for multi-dimensional arrays is
to provide clarity and ease of use. If you aren't going to use the
multi-dimensional nature of a in the function, why ever declare it that
way in the first place? Just make it:

int a[12];

What great problem do you think you're solving with this?

Brian

Hi Brian,

Thanks for the reply.
To answer your question I was just curious to know whether its
just a compiler gimmick to ensure clarity or is there any other hidden
reason for the same.

Thanks
Sac
 
D

Default User

Thanks for the reply.
To answer your question I was just curious to know whether its
just a compiler gimmick to ensure clarity or is there any other hidden
reason for the same.

The main reason for multi-dimensional arrays (although technically they
aren't in C) is for ease of use. Because the stride of the pointer is
adjusted for its type, it saves the programmer extra pointer
arithmetic, which can be a source of bugs.

If you have:

void f(int a[][4]);

You can iterate over the subarrays with:

void f(int a[][4], size_t size)
{
size_t i;

for (i = 0; i < size; i++)
{
/* do stuff with a */
}
}

If a were just an int*, you'd have to have another size passed in (or
know it from macros or global information) and calculate that offset
into the array manually.




Brian
 
B

Barry Schwarz

snip
The main reason for multi-dimensional arrays (although technically they
aren't in C) is for ease of use. Because the stride of the pointer is

Since the standard says they are (6.5.2.1-3), technically they are.

snip
 
J

James Kuyper

Barry said:
snip


Since the standard says they are (6.5.2.1-3), technically they are.

Yes, but only "technically". :) To truly understand them, you need to
be aware of the fact that what C misleadingly calls a multidimensional
array is actually just an array of arrays. If "int a[10][15]" defined a
truly multidimensional array, then the element type of 'a' would be
'int', and there would be some concept (call it "shape") which describes
the [10][15] part of the declaration as a thing in itself, separate from
the. As far as the C standard is concerned, 'a' is a 10 element array
with an element type of 'int[15]', which has far-ranging effects on
things like pointer arithmetic.

If C "multidimensional" arrays were actually multi-dimensional, it would
be just as easy to write an expression that refers to a column of an
array as it is to create one that refers to a row of the array.
 
K

Keith Thompson

James Kuyper said:
Barry said:
Since the standard says they are (6.5.2.1-3), technically they are.

Yes, but only "technically". :) To truly understand them, you need to
be aware of the fact that what C misleadingly calls a multidimensional
array is actually just an array of arrays. If "int a[10][15]" defined
a truly multidimensional array, then the element type of 'a' would be
'int', and there would be some concept (call it "shape") which
describes the [10][15] part of the declaration as a thing in itself,
separate from the. As far as the C standard is concerned, 'a' is a 10
element array with an element type of 'int[15]', which has far-ranging
effects on things like pointer arithmetic.

If C "multidimensional" arrays were actually multi-dimensional, it
would be just as easy to write an expression that refers to a column
of an array as it is to create one that refers to a row of the array.

You're making a lot of assumptions about what the phrase
"multidimensional array" means. The C standard doesn't happen to make
those assumptions.

You could as easily argue that C doesn't *really* have enumeration
types, since C's enum feature lacks a lot of the features you'd
expect.

And I've worked in languages that have multidimentionsional arrays,
distinct from arrays of arrays, that *don't* let you refer to columns,
and that don't have a concept of "shape" that you've described above.
There's enough variation among the features that different languages
support for multidimensional arrays that you can't reasonably draw a
line between true multimensional arrays and fake ones.

C really does have multidimensional arrays. They're simply arrays of
arrays. There are reasons to dislike this state of affairs, but
that's the way it is.
 
C

CBFalconer

Barry said:
snip


Since the standard says they are (6.5.2.1-3), technically they are.

snip

Here is what the standard says. I don't understand your stmnt.

6.5.2.1 Array subscripting

Constraints

[#1] One of the expressions shall have type ``pointer to
object type'', the other expression shall have integer type,
and the result has type ``type''.

Semantics

[#2] A postfix expression followed by an expression in
square brackets [] is a subscripted designation of an
element of an array object. The definition of the subscript
operator [] is that E1[E2] is identical to (*((E1)+(E2))).
Because of the conversion rules that apply to the binary +
operator, if E1 is an array object (equivalently, a pointer
to the initial element of an array object) and E2 is an
integer, E1[E2] designates the E2-th element of E1 (counting
from zero).

[#3] Successive subscript operators designate an element of
a multidimensional array object. If E is an n-dimensional
array (n>=2) with dimensions i+j+ ... +k, then E (used as
other than an lvalue) is converted to a pointer to an
(n-1)-dimensional array with dimensions j+ ... +k. If the
unary * operator is applied to this pointer explicitly, or
implicitly as a result of subscripting, the result is the
pointed-to (n-1)-dimensional array, which itself is
converted into a pointer if used as other than an lvalue.
It follows from this that arrays are stored in row-major
order (last subscript varies fastest).

[#4] EXAMPLE Consider the array object defined by the
declaration
int x[3][5];

Here x is a 3+5 array of ints; more precisely, x is an array
of three element objects, each of which is an array of five
ints. In the expression x, which is equivalent to
(*((x)+(i))), x is first converted to a pointer to the
initial array of five ints. Then i is adjusted according to
the type of x, which conceptually entails multiplying i by
the size of the object to which the pointer points, namely
an array of five int objects. The results are added and
indirection is applied to yield an array of five ints. When
used in the expression x[j], that array is in turn
converted to a pointer to the first of the ints, so x[j]
yields an int.
 
B

Barry Schwarz

Since the standard says they are (6.5.2.1-3), technically they are.

Here is what the standard says.  I don't understand your stmnt.

snip

  [#3] Successive subscript operators designate an element  of
  a  multidimensional  array object.  If E is an n-dimensional

snip

The standard says there is something called a multidimensional array
object. What more is required to refute the statement that they don't
exist in C?
 
C

CBFalconer

Barry said:
.... snip ...
Here is what the standard says. I don't understand your stmnt.
snip

[#3] Successive subscript operators designate an element of
a multidimensional array object. If E is an n-dimensional

snip

The standard says there is something called a multidimensional
array object. What more is required to refute the statement
that they don't exist in C?

But that segment is simply a description of how a multi-dim array
is mapped into and actual single-dim array.
 
K

Keith Thompson

CBFalconer said:
Barry said:
... snip ...
Here is what the standard says. I don't understand your stmnt.
snip

[#3] Successive subscript operators designate an element of
a multidimensional array object. If E is an n-dimensional

snip

The standard says there is something called a multidimensional
array object. What more is required to refute the statement
that they don't exist in C?

But that segment is simply a description of how a multi-dim array
is mapped into and actual single-dim array.

You snipped the statement in question. The point is that C does have
multidimensional arrays (which happen to be arrays of arrays); the
standard clearly says so.
 
C

CBFalconer

Keith said:
CBFalconer said:
Barry said:
... snip ...

Here is what the standard says. I don't understand your stmnt.

snip

[#3] Successive subscript operators designate an element of
a multidimensional array object. If E is an n-dimensional

snip

The standard says there is something called a multidimensional
array object. What more is required to refute the statement
that they don't exist in C?

But that segment is simply a description of how a multi-dim array
is mapped into and actual single-dim array.

You snipped the statement in question. The point is that C does
have multidimensional arrays (which happen to be arrays of arrays);
the standard clearly says so.

Cavil: I didn't snip it. Mr Schwarz did. :)
 
K

Keith Thompson

CBFalconer said:
Keith Thompson wrote: [snip]
You snipped the statement in question. The point is that C does
have multidimensional arrays (which happen to be arrays of arrays);
the standard clearly says so.

Cavil: I didn't snip it. Mr Schwarz did. :)

That turns out not to be correct. The statement in question was

The main reason for multi-dimensional arrays (although technically
they aren't in C) is for ease of use.

This appeared as part of the quoted material in Barry Schwarz's
article, but not in your immediate followup.
 
C

CBFalconer

Keith said:
CBFalconer said:
Keith Thompson wrote:
[snip]
You snipped the statement in question. The point is that C does
have multidimensional arrays (which happen to be arrays of arrays);
the standard clearly says so.

Cavil: I didn't snip it. Mr Schwarz did. :)

That turns out not to be correct. The statement in question was

The main reason for multi-dimensional arrays (although technically
they aren't in C) is for ease of use.

This appeared as part of the quoted material in Barry Schwarz's
article, but not in your immediate followup.

Well, it seems to appear in the copy of my followup here.

Message-ID: <[email protected]>

It's not really crucial. :)
 
K

Keith Thompson

CBFalconer said:
Keith said:
CBFalconer said:
Keith Thompson wrote:
[snip]

You snipped the statement in question. The point is that C does
have multidimensional arrays (which happen to be arrays of arrays);
the standard clearly says so.

Cavil: I didn't snip it. Mr Schwarz did. :)

That turns out not to be correct. The statement in question was

The main reason for multi-dimensional arrays (although technically
they aren't in C) is for ease of use.

This appeared as part of the quoted material in Barry Schwarz's
article, but not in your immediate followup.

Well, it seems to appear in the copy of my followup here.

Message-ID: <[email protected]>

It also appeared in Barry Schwarz's followup to that message:

<[email protected]>

but not in your followup to Barry's message:

It's not really crucial. :)

True.
 
B

Barry Schwarz

Keith said:
CBFalconer said:
Keith Thompson wrote:
You snipped the statement in question.  The point is that C does
have multidimensional arrays (which happen to be arrays of arrays);
the standard clearly says so.
Cavil: I didn't snip it.  Mr Schwarz did.  :)
That turns out not to be correct.  The statement in question was
  The main reason for multi-dimensional arrays (although technically
  they aren't in C) is for ease of use.
This appeared as part of the quoted material in Barry Schwarz's
article, but not in your immediate followup.

Well, it seems to appear in the copy of my followup here.  

           Message-ID: <[email protected]>

It's not really crucial.  :)

Since it is the only statement in the original post that my post
addressed, omitting it from the follow-on discussion begs the question
of why take issue with my response at all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top