Re: Size of array's dimensions
- From: "Doug Harrison [MVP]" <dsh@xxxxxxxx>
- Date: Tue, 31 Jan 2006 10:17:04 -0600
On Mon, 30 Jan 2006 20:52:29 -0800, "Z" <Z@xxxxxxxxxxxxxxxxxxxxxxxxx>
wrote:
>> On Mon, 30 Jan 2006 20:34:21 -0800, "Z" <Z@xxxxxxxxxxxxxxxxxxxxxxxxx>
>> wrote:
>>
>> >Hi,
>> >
>> >How do I get the size of any dimension of a multidimensional array with the
>> >sizeof operator?
>> >If I have int my_array[M][N] and I use sizeof(my_array) I get the size of
>> >the whole array (all dimensions). How do I get the size of one dimension only?
>>
>No, M and N are not known
If you don't know N, you can't use sizeof, because you don't have an array;
instead, you have a pointer to a pointer, for which sizeof won't give you
the answer you seek. If you know N but don't know M, you have one of three
things:
1. An array x[][N] whose initialization is in scope.
2. An array x[][N] whose initialization is not in scope.
3. A function parameter x[][N].
You can use sizeof to determine M only for (1). For (2), you have an array,
but the compiler doesn't know its size and will actually reject attempts to
use sizeof to determine M. For (3), you have a pointer, not an array, and
sizeof will give the wrong answer.
You got a couple of correct answers elsewhere in the thread, but I'm
wondering why "M and N are not known". I'm having a hard time imagining
scenarios in which sizeof can help you determine M and N for an array and
you can't say M and N directly. If M and N are complicated constant
expressions, I'd make them named constants. If you're writing a macro or
some sort of injected code, be sure you use it only on actual arrays; for
this to work, M and N actually are known, but for some bizarre reason, your
code doesn't know their names, e.g. they're not represented as macro
arguments.
--
Doug Harrison
Visual C++ MVP
.
- References:
- Re: Size of array's dimensions
- From: Lilith
- Re: Size of array's dimensions
- Prev by Date: Re: initialize array within a class by a class member
- Next by Date: Re: request to solve my problem
- Previous by thread: Re: Size of array's dimensions
- Next by thread: Re: Size of array's dimensions
- Index(es):
Relevant Pages
|
|