Re: A Certain Size/sizeof

From: Ulrich Eckhardt (doomster_at_knuut.de)
Date: 11/14/03


Date: Fri, 14 Nov 2003 21:54:34 +0100

Gene Wirchenko wrote:
> int BufferTestNumbers[][17]=
> { ... };
>
> std::string
> TestBuffer[sizeof(BufferTestNumbers)/sizeof(int)/17];
>
> Truly an ugly array size expression, no?

Firstly, sizeof is not a function, therefore 'sizeof BufferTestNumbers'
doesn't need brackets. 'sizeof (int)' is an example the second version to
invoke that operator and that takes a typename in brackets, but still not
a function.

> Is there a way to declare TestBuffer without the magic number? I
> would prefer to keep the declaration of BufferTestNumbers as it is (or
> with very minor changes) as it is easy to adjust should I want other
> test cases. I ask for both C and C++.

assuming I had
  int array[] = { 1,2,3,4,5,8,12 };
I usually do
  size_t const count = sizeof array / sizeof *array;

Adapting that to a more-dimensional array is left as exercise for
the reader.

Uli

-- 
Questions ?
see  C++-FAQ Lite: http://parashift.com/c++-faq-lite/  first !


Relevant Pages

  • Re: How can I get the size of this?
    ... >>>I am now having a problem at using sizeof() function ... Again I *prefer* return without brackets - unless of course I'm ... evaluating something in which the brackets make it clear that the ... David Webber ...
    (microsoft.public.vc.language)
  • Re: Technical reason why sizeof() is an operator and not a function?
    ... Shiver. ... That's irrelevant to the question of whether sizeof should take ... brackets. ... int pointers don't usually change. ...
    (comp.programming)
  • Re: sizeof(x) in C and in C++
    ... >> Of course you can't find it, sizeof() is a C operator not a function. ... I'd like to amend that even the brackets are optional. ... > prototype. ...
    (comp.lang.c)
  • Re: Flexible arrays - v confused
    ... When you are in the habit of omitting the brackets for "sizeof var", ... I use space in 'sizeof ' so that sizeof operator cannot so easily be confused with a function. ...
    (comp.lang.c)