Re: [C] Array bounds in function prototypes
From: Andrey Tarasevich (andreytarasevich_at_hotmail.com)
Date: 03/11/04
- Next message: Leor Zolman: "Re: [C] increment char* and indirection"
- Previous message: Josh Sebastian: "Re: [C++] Accelerated C++ Ex 8.3"
- In reply to: Jason Spashett: "Re: [C] Array bounds in function prototypes"
- Next in thread: Andrey Tarasevich: "Re: [C] Array bounds in function prototypes"
- Reply: Andrey Tarasevich: "Re: [C] Array bounds in function prototypes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 10 Mar 2004 15:55:15 -0800
Jason Spashett wrote:
> ...
>> Well, just like it says, an array argument 'decays' into a pointer
> ...
>
> Thanks for the detailed information. However, exactly why does it decay to a
> pointer in a function prototype? I can't see why the array typing
> information could not be preserved, is it for backwards compatibility? [ And
> as an aside: is this the same in C++, I would presume not ]
If you preserve the array typing, then the syntax that you used will
formally mean that the array is passed "by value", i.e. the function
receives the _copy_ of the argument array. But in both C and C++ array
are neither copyable nor assignable. (Unfortunately I don't remember the
exact rationale behind the original limitation). This means that "pass
by value" syntax for arrays in C/C++ should be either outlawed or forced
to mean something else. In the current C/C++ it is the latter, as was
already explained by Mike.
The workaround that will preserve the array typing is to pass the array
"by pointer"
void fn(unsigned char (*map)[256]) {
printf("%u", sizeof *map );
}
or, in C++, "by reference"
void fn(unsigned char (&map)[256]) {
printf("%u", sizeof *map );
}
> I realised that I could pass a pointer to an array of n elements to do the
> job, I've never actually passed fixed sized array before. I normally just
> say char *map instead.
Using 'char* map' or 'char map[]' is just a useful trick that allows you
to declare a function that will be able to work with arrays of different
sizes. If the function is supposed to work only with arrays of one
pre-determined size, then the better idea would be to use either "pass
by pointer" or "pass by reference" approach, since they offer better
type safety.
> In the case of printf("%u", sizeof x); won't x get promoted to unsigned int
> anyway, even if one does cast a size_t type to some unsigned type it may not
> work anyway (in theory) ?
Sorry, I don't understand the question.
-- Best regards, Andrey Tarasevich
- Next message: Leor Zolman: "Re: [C] increment char* and indirection"
- Previous message: Josh Sebastian: "Re: [C++] Accelerated C++ Ex 8.3"
- In reply to: Jason Spashett: "Re: [C] Array bounds in function prototypes"
- Next in thread: Andrey Tarasevich: "Re: [C] Array bounds in function prototypes"
- Reply: Andrey Tarasevich: "Re: [C] Array bounds in function prototypes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|