Re: Size of array's dimensions



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
.



Relevant Pages

  • Re: [C or C++] Is this legal? sizeof *p
    ... where the size of an automatic array is defined at run time. ... > that case if sizeof is applied to a VLA, ... But applying sizeof to a VLA is applying ... > sizeof to an array, not a pointer, and using the name of an array as ...
    (alt.comp.lang.learn.c-cpp)
  • Re: How can I get the size of this?
    ... > I am now having a problem at using sizeof() function ... the pointer. ... Operating on something declared as an array returns the size of the ... LPCSTR is already typedef'd as "const char *", ...
    (microsoft.public.vc.language)
  • Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)bebanned?
    ... Using SizeOf on variables does not prevent programming mistakes. ... How will you solve the problem for pointer variables? ... For that, I use dynamic arrays, which case I use Length in conjunction with SizeOf to determine the amount of memory occupied by the array data. ...
    (alt.comp.lang.borland-delphi)
  • Re: De-referencing pointer to function-pointer
    ... >> Either your compiler is broken or you are not invoking it as a C ... >> is a constraint violation to apply the sizeof operator to a function ... >> pointer to the element type of the array. ... >> bytes, of the function, not of a pointer to the function. ...
    (comp.lang.c)
  • Re: A question on string literals
    ... and is why I said "or maybe even `sizeof context'". ... "pointer to T"; ... to first element of array". ... Chris Torek, Wind River Systems ...
    (comp.lang.c)