Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #101827

Re: sizeof(x) / sizeof(x[0])

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: sizeof(x) / sizeof(x[0])
Date 2017-01-14 15:52 -0800
Organization None to speak of
Message-ID <ln8tqdi4hd.fsf@kst-u.example.com> (permalink)
References <1420cd8f-e5cb-4a8f-93e6-d80a8b050650@googlegroups.com>

Show all headers | View raw


Alla _ <modelling.data@gmail.com> writes:
[...]
> char *array[] = {"abc", "defg", "hijklmop", NULL};

`array` is a 4-element array of char* pointers.  (This would be easier
to talk about if it had a name other than "array".  I'll use backticks
to keep things straight.)

> The pointer points to an array that has 4 strings. 

What pointer?  `array` is an array object, not a pointer object.
(When used in an expression, the name does decay to a pointer value
in some but not all contexts, but that's not relevant here; the
operand of `sizeof` is one of the contexts in which that conversion
does not occur.)

It is not an "array of strings".  Strictly speaking, there is no such
thing as an "array of strings", since "string" is not a type.  It is an
array of 4 char* pointers.  The first 3 of those pointers happen to be
pointers to strings; the last is not, since it's a null pointer.

`array` does not contain strings.  It contains pointers to strings (plus
one pointer that isn't a pointer to a string).  But that's not relevant
to how `sizeof` works.

See N1570 7.1.1p1 for the definitions of "string" and "pointer to
string".

> sizeof(x) here will be sizeof(array) thus indicating 
> the size in bytes of the whole array of strings, 

This is the first time you've mentioned "x".  I guess you have a macro
whose definition you haven't shown us.  Most likely the macro is
something like this:

    #define ARRAY_LENGTH(x) (sizeof (x) / sizeof (x)[0])

invoked as

    ARRAY_LENGTH(array)

(I used extra parentheses that I wouldn't have used outside a macro
definition.)

> because 'array' here is used as the address of the
> first sub-element, which is 'a' (array is the same as
> array[0][0]); 

No, `array` in `sizeof(array)` is not being used as an address.
It's being used as the name of an object.  And no, `array` is not the
same as `array[0][0]`.

> so sizeof(array) will give the number of bytes occupied
> by the whole array of strings, i.e. number
> of characters x 1 (which is the size of a char);

`sizeof(array)` is the number of bytes occupied by the array object.
It is 4 * sizeof (char*).

> the denominator sizeof(x[0]) gives the size of the 
> first element of the array, i.e. the size of the first 
> string, which is here "abc", so it it is 3 (3 characters,
> each 1 byte); and all other strings have different
> number of characters in them, and hence different
> number of bytes;

Not at all.  `sizeof (x[0])`, which expands to `sizeof (array[0])`,
is the size of the first element of the array, which is a pointer
object.  (It's likely to be 8 on a 64-bit system.)  It has nothing
to do with the length of the string to which `array[0]` happens
to point.

> how then sizeof(x) / sizeof(x[0]) gives a correct number
> of strings in the array (it actually does, I have tested it
> on different strings)? Please, show me my mistake. 

It gives the number of pointer objects in the object `array`, which is
an array of `char*` pointers.  If `sizeof (char*) == 8`, then `sizeof
(array)` is 32, and `sizeof (array[0])` is 8, so `ARRAY_LENGTH(array)`
is 4.

(And of course the ARRAY_LENGTH() macro won't work with a pointer object,
even if that pointer object is a parameter defined with array notation.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

sizeof(x) / sizeof(x[0]) Alla _ <modelling.data@gmail.com> - 2017-01-14 04:16 -0800
  Re: sizeof(x) / sizeof(x[0]) "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 04:27 -0800
    Re: sizeof(x) / sizeof(x[0]) Alla _ <modelling.data@gmail.com> - 2017-01-14 04:43 -0800
      Re: sizeof(x) / sizeof(x[0]) "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 04:52 -0800
        Re: sizeof(x) / sizeof(x[0]) Alla _ <modelling.data@gmail.com> - 2017-01-14 06:10 -0800
        Re: sizeof(x) / sizeof(x[0]) Keith Thompson <kst-u@mib.org> - 2017-01-14 15:55 -0800
  Re: sizeof(x) / sizeof(x[0]) jt@toerring.de (Jens Thoms Toerring) - 2017-01-14 13:13 +0000
    Re: sizeof(x) / sizeof(x[0]) Alla _ <modelling.data@gmail.com> - 2017-01-15 22:23 -0800
      Re: sizeof(x) / sizeof(x[0]) Keith Thompson <kst-u@mib.org> - 2017-01-16 08:58 -0800
  Re: sizeof(x) / sizeof(x[0]) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 14:47 +0000
    Re: sizeof(x) / sizeof(x[0]) Alla _ <modelling.data@gmail.com> - 2017-01-14 07:17 -0800
  Re: sizeof(x) / sizeof(x[0]) Manfred <noname@invalid.add> - 2017-01-14 16:22 +0100
  Re: sizeof(x) / sizeof(x[0]) Barry Schwarz <schwarzb@dqel.com> - 2017-01-14 08:35 -0800
    Re: sizeof(x) / sizeof(x[0]) Alla _ <modelling.data@gmail.com> - 2017-01-15 22:53 -0800
      Re: sizeof(x) / sizeof(x[0]) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-16 15:58 +0000
        Re: sizeof(x) / sizeof(x[0]) Alla _ <modelling.data@gmail.com> - 2017-01-16 22:38 -0800
          Re: sizeof(x) / sizeof(x[0]) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-17 10:11 +0000
  Re: sizeof(x) / sizeof(x[0]) James Kuyper <jameskuyper@verizon.net> - 2017-01-14 11:43 -0500
    Re: sizeof(x) / sizeof(x[0]) Keith Thompson <kst-u@mib.org> - 2017-01-14 16:01 -0800
    Re: sizeof(x) / sizeof(x[0]) Alla _ <modelling.data@gmail.com> - 2017-01-16 01:36 -0800
      Re: sizeof(x) / sizeof(x[0]) Barry Schwarz <schwarzb@dqel.com> - 2017-01-16 09:47 -0800
      Re: sizeof(x) / sizeof(x[0]) James Kuyper <jameskuyper@verizon.net> - 2017-01-16 21:12 -0500
        Re: sizeof(x) / sizeof(x[0]) Alla _ <modelling.data@gmail.com> - 2017-01-16 22:29 -0800
  Re: sizeof(x) / sizeof(x[0]) Les Cargill <lcargill99@comcast.com> - 2017-01-14 14:27 -0600
  Re: sizeof(x) / sizeof(x[0]) Keith Thompson <kst-u@mib.org> - 2017-01-14 15:52 -0800
  Re: sizeof(x) / sizeof(x[0]) Lőrinczy Zsigmond <nospam@for.me> - 2017-01-15 20:20 +0100
    Re: sizeof(x) / sizeof(x[0]) Keith Thompson <kst-u@mib.org> - 2017-01-15 12:55 -0800

csiph-web