Re: [C] functions and 2D arrays?
From: Leor Zolman (leor_at_bdsoft.com)
Date: 01/09/04
- Previous message: forums_mp: "Re: reference counting semantics more .."
- In reply to: Arthur J. O'Dwyer: "Re: [C] functions and 2D arrays?"
- Next in thread: Arthur J. O'Dwyer: "Re: [C] functions and 2D arrays?"
- Reply: Arthur J. O'Dwyer: "Re: [C] functions and 2D arrays?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 09 Jan 2004 01:56:42 GMT
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 */
pfirstp(a); /* warning */
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.
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.
-leor
Leor Zolman
BD Software
leor@bdsoft.com
www.bdsoft.com
C++ users: Download BD Software's free STL Error Message
Decryptor at: www.bdsoft.com/tools/stlfilt.html
- Previous message: forums_mp: "Re: reference counting semantics more .."
- In reply to: Arthur J. O'Dwyer: "Re: [C] functions and 2D arrays?"
- Next in thread: Arthur J. O'Dwyer: "Re: [C] functions and 2D arrays?"
- Reply: Arthur J. O'Dwyer: "Re: [C] functions and 2D arrays?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|