Re: [C] functions and 2D arrays?

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 01/09/04


Date: Thu, 8 Jan 2004 21:25:50 -0500 (EST)


On Fri, 9 Jan 2004, Leor Zolman wrote:
>
> On Thu, 8 Jan 2004 19:16:21 -0500 (EST), "Arthur J. O'Dwyer" >to
> >
> > Also, you've got one more layer of indirection there than you
> >really need. There's no reason to be passing around *pointers*
> >to arrays of arrays of int, when you could be passing pointers
> >to arrays of int.
>
> Absolutely. When reading the OP's question, my first thought was just
> that exactly; I had meant to say something about how using a "pointer
> to array" is rather out of the ordinary. Enough so that most compilers
> accept &array as an expression in lieu of just "array" (though they
> may emit warnings), and even vice-versa. For example, VC++ 7.1 accepts
> this:
>
> #include <stdio.h>
>
> void pfirst(int a[10])
> {
> printf("a[0] = %d\n", a[0]);
> }
>
> void pfirstp(int (*a)[10])
> {
> printf("(*a)[0] = %d\n", (*a)[0]);
> }
>
> int main()
> {
> int a[10] = {1,2,3};
> pfirst(a);
> pfirst(&a); /* warning */

  That's fine. I mean, it's fine that the compiler emits a diagnostic
for it. The standard doesn't require the compiler to *refuse* to compile
that code; it just says, "You must warn the user when he does something
stupid." The code is broken; the compiler's fine.

> pfirstp(a); /* warning */

  Ditto. Broken code, extra-lenient compiler.

> pfirstp(&a);
> return 0;
> }
>
> Comeau, on the other hand, flags those both as fatal errors in one of
> its "strict" modes (which seems to be the default in my configuration)
> but just gives warnings in C90 or C99 mode. MSVC probably has similar
> options.

  Maybe. Don't count on it. :-)

> Although passing a "pointer to array" is in some sense more
> straight-forward than taking advantage of the way array expressions
> "decay" into pointers, it is nevertheless the latter approach that
> feels more "natural" after you've been reading and writing C and C++
> for a while. Go figure.

  Probably because it's the correct way. You must know by now that
if 'a' is defined as an array[10] of int, then the expression 'a' in
a value context is of type 'pointer to int' ("The Rule"), and the
expression '&a' is of type 'pointer to array[10] of int' (my personal
most-important-rule-of-C). And those are two different pointer types,
and never the twain shall meet, without an appropriate compiler
diagnostic.

-Arthur



Relevant Pages

  • Re: Program speed execution question
    ... It might also be cache size, memory speed, total memory, or other features of the two machines, or it could be that the compiler optimizations are different for the two machines. ... With pointer arrays, the compiler must often assume that the arrays can overlap through hidden aliases, and this inhibits many optimizations. ...
    (comp.lang.fortran)
  • Re: Arrays and Functions (how to clean up code)
    ... int main{ ... If you change meqn and mx to #define macros, ... for variable length arrays. ... compiler diagnostic is required. ...
    (comp.lang.c)
  • Re: why cannot assign to function call
    ... and passing the array by reference? ... void mysub (int k, int n) { ... say C was definitely pass-by-reference (for arrays)? ... If one could only use v as a pointer (i.e. access ...
    (comp.lang.python)
  • Re: Problem bei make
    ... in vielen Faellen array und Pointer gleichzusetzen (auch wenn es ... Den Unterschied zwischen arrays und Pointern (auch konstanten ... Hersteller koennte seinen Compiler also durchaus Code erzeugen lassen, ... der bei solcherlei Pointer-Arithmetik zum Programmabbruch fuehrt und ...
    (de.comp.os.unix.linux.misc)
  • Two dimensional arrays "C question"
    ... My question has to do with two dimensional arrays. ... int main ... So we see that the compiler can automatically calculate the first dimension. ...
    (microsoft.public.vc.language)