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


Groups > comp.lang.c > #101970

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

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: sizeof(x) / sizeof(x[0])
Date 2017-01-16 15:58 +0000
Organization A noiseless patient Spider
Message-ID <87eg03hu82.fsf@bsb.me.uk> (permalink)
References <1420cd8f-e5cb-4a8f-93e6-d80a8b050650@googlegroups.com> <tmjk7cppga86gcc0022rvsrb8c84au0e0n@4ax.com> <d96856b2-263a-45a4-a0ab-41d7ed5c9655@googlegroups.com>

Show all headers | View raw


Alla _ <modelling.data@gmail.com> writes:

> On Saturday, January 14, 2017 at 7:35:56 PM UTC+3, Barry Schwarz wrote:
>> On Sat, 14 Jan 2017 04:16:30 -0800 (PST), Alla _
>> <modelling.data@gmail.com> wrote:
>> 
>> <snip>
>> >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!  The sizeof operator is an exception to the rule that an array
>> name is converted to a pointer to the first element.

I think the context here is

  char *array[] = { "abc", "def", "..." };

>> In the non-exceptional case, the conversion would be equivalent to
>> &array[0] which would be the address of (not the address contained in)
>> the first pointer.  The byte (not char) at that address would be
>> either the high order or low order byte of the address contained in
>> the pointer and most likely would not correspond to any printable
>> character.  &array[0] would not point to the 'a' in "abc".
>
> I am not sure I understood this correctly. You say that in normal,
> non-exceptional case, &array[0] doesn't guarantee to point to the 
> first element in the first string?

Yes, that's what he's saying.  array is an array of pointers, so
array[0] is the first pointer and &array[0] is a pointer to that first
pointer.

> &array[0] does point to the first string,
> correct? Where else can it point to given the index?

It points to the first element of the array.

> Could you, please, give an example with hexadecimal addresses, so 
> I can see what can happen. Please.

The compiler will make this structure:

0x100: 'a' 'b' 'c'  0
0x104: 'd' 'e' 'f'  0
0x108: '.' '.' '.'  0
0x10c: 0x100
0x110: 0x104
0x114: 0x108

The name `array` will be associated with the address 0x10c.  The
following expressions have the values and types show:

  expression   value    type
  array        0x10c    char **
  array[0]     0x104    char *
  array[0][0]  'a'      char
  &array       0x10c    char (*)[4]
  &array[0]    0x10c    char **
  &array[0][0] 0x104    char *

The types are important because a C pointer is an address with a
specific type.

-- 
Ben.

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