Re: [C] functions and 2D arrays?
From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 01/09/04
- Next message: Leor Zolman: "Re: [C++] Functions Opinion"
- Previous message: Leor Zolman: "Re: [C] functions and 2D arrays?"
- In reply to: Leor Zolman: "Re: [C] functions and 2D arrays?"
- Next in thread: Leor Zolman: "Re: [C] functions and 2D arrays?"
- Reply: Leor Zolman: "Re: [C] functions and 2D arrays?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Leor Zolman: "Re: [C++] Functions Opinion"
- Previous message: Leor Zolman: "Re: [C] functions and 2D arrays?"
- In reply to: Leor Zolman: "Re: [C] functions and 2D arrays?"
- Next in thread: Leor Zolman: "Re: [C] functions and 2D arrays?"
- Reply: Leor Zolman: "Re: [C] functions and 2D arrays?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|