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


Groups > comp.lang.c > #163952

Re: on understanding & and pointer arithmetic

Path csiph.com!aioe.org!w1hPHPb/YrPziOBjHD1tGA.user.46.165.242.75.POSTED!not-for-mail
From Meredith Montgomery <mmontgomery@levado.to>
Newsgroups comp.lang.c
Subject Re: on understanding & and pointer arithmetic
Date Sat, 18 Dec 2021 12:09:05 -0300
Organization Aioe.org NNTP Server
Message-ID <867dc26jce.fsf@levado.to> (permalink)
References <86a6h0eiy9.fsf@levado.to> <spfi5g$6ko$1@dont-email.me> <865yrod2cx.fsf@levado.to> <87h7b8wind.fsf@bsb.me.uk> <86ee6c9w1a.fsf@levado.to> <87wnk4utcs.fsf@bsb.me.uk>
Mime-Version 1.0
Content-Type text/plain
Injection-Info gioia.aioe.org; logging-data="59564"; posting-host="w1hPHPb/YrPziOBjHD1tGA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice Filtered by postfilter v. 0.9.2
Cancel-Lock sha1:Lxs7ljd1UPTO4riqJIOjkZ4Xmnc=
Xref csiph.com comp.lang.c:163952

Show key headers only | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> Meredith Montgomery <mmontgomery@levado.to> writes:
>
>> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>
>>> Meredith Montgomery <mmontgomery@levado.to> writes:
>>>
>>>> Bart <bc@freeuk.com> writes:
>>>>> On 16/12/2021 14:13, Meredith Montgomery wrote:
>>>
>>>>>> I also
>>>>>> know that /&a/ is the same as /a/.
>>>>>
>>>>> The same /what/? Those have the same values but different types
>>>>> ((T*)[] vs T* where T is a's element type).
>>>
>>> In this specific case, char (*)[4] and char *.  (T*)[] is not a valid
>>> type (for some base type T), and even with the syntax corrected (it's
>>> T(*)[]), it's still wrong because the size is missing.  C does have
>>> array types with no size, but none are relevant to your case.
>>
>> Thanks for the correction.  I'll repeat what you say in my words just to
>> get a chance that you might verify what I'm saying.  When I have an
>> array such as 
>>
>>   char a[] = "hello, world"
>>
>> which occupies 13 bytes, then a pointer to it is a pointer of type
>>
>>   char (*a)[13]
>>
>> and that's how we write its type --- although I could omit the letter a
>> there, perhaps I should, I also think it's okay to write it there.
>
> Not quite.  When you write a type (for example, to use in a cast
> operator) you must omit the name.  If you include a name, then what you
> are writing a declaration, not a type.
>
> So, the object `a` has type char [13].  When used in a most contexts,
> `a` is converted to an expression of type char *, but when you apply the
> & operator, this conversion is not done and the pointer you get is of
> type char (*)[13].

Understood.

[...]

>>>> #include <stdio.h>
>>>> int main() {
>>>>   char a[] = "abc";
>>>>   printf("a: %lu\n", a);
>>>>   printf("a: %lu\n", a + 3);
>>>>   printf("a: %lu\n", &a + 3);
>>>> }
>>>
>>> With any luck, you compiler should have had lots to say about this
>>> code.  If it did not, turn up the warnings.  I'll write it my way with
>>> comments.
>>
>> Yes, the warnings were there.  In my new experiment I added casts, but
>> kept the %lu because I wanted to see things in base 10.
>
> OK, so you converted the pointers to unsigned long.  That will often
> work, but "modern" C (since 1999), has an integer type that, if the
> conversion is possible at all, is guaranteed to be the right width:
> uintptr_t.  It, along with a whole lot of other useful types, is
> declared in <stdint.h>.
>
> You might ask, how does one print such a type?  Well there is another
> header, <inttypes.h>, that defines macros with the right width:
>
>   printf("a is at %"PRIuPTR"\n", (uintptr_t)a);
>
> A bit of a mouthful, but the result will work on any system where
> pointers can be converted to some unsigned integer type.  (There is also
> a signed integer type intptr_t and a corresponding macro, PRIiPTR, for
> printing it.)

Thanks.  Not long ago, I think I myself initiated some thread where I
learned about these PRI* constants for printf and where to find
documentation for them.  But the uintptr_t is new to me.  Thanks for the
info.

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


Thread

on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-16 11:13 -0300
  Re: on understanding & and pointer arithmetic Bart <bc@freeuk.com> - 2021-12-16 14:23 +0000
    Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-16 11:57 -0300
      Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 17:42 +0000
        Re: on understanding & and pointer arithmetic Bart <bc@freeuk.com> - 2021-12-16 18:14 +0000
          Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 21:14 +0000
        Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-16 16:42 -0300
          Re: on understanding & and pointer arithmetic Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-16 12:44 -0800
            Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:06 -0300
          Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 21:34 +0000
            Re: on understanding & and pointer arithmetic Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-16 14:14 -0800
              Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 23:44 +0000
            Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:09 -0300
  Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-16 16:23 +0100
    Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-16 16:25 -0300
      Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-16 21:08 +0100
        Re: on understanding & and pointer arithmetic Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-16 12:54 -0800
          Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-17 10:53 +0100
            Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 11:39 -0300
              Re: on understanding & and pointer arithmetic scott@slp53.sl.home (Scott Lurndal) - 2021-12-18 16:31 +0000
              Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-18 18:56 +0100
                Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-25 21:29 -0300
              Re: on understanding & and pointer arithmetic Bart <bc@freeuk.com> - 2021-12-18 18:29 +0000
        Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 11:32 -0300
    Re: on understanding & and pointer arithmetic Manfred <noname@add.invalid> - 2021-12-16 21:34 +0100
      Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 22:20 +0000
        Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:02 -0300
          Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:34 -0300
          Re: on understanding & and pointer arithmetic Manfred <noname@add.invalid> - 2021-12-18 18:36 +0100
          Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-18 21:17 +0000
      Re: on understanding & and pointer arithmetic James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-12-16 18:59 -0500
  Re: on understanding & and pointer arithmetic James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-12-16 18:38 -0500
    Re: on understanding & and pointer arithmetic Bart <bc@freeuk.com> - 2021-12-16 23:50 +0000
      Re: on understanding & and pointer arithmetic "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2021-12-17 07:34 -0800
        Re: on understanding & and pointer arithmetic Bart <bc@freeuk.com> - 2021-12-17 17:48 +0000
    Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 23:56 +0000
      Re: on understanding & and pointer arithmetic James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-12-16 19:06 -0500
        Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-17 00:10 +0000
        Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:32 -0300
    Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:30 -0300
  Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-17 11:08 +0100
    Re: on understanding & and pointer arithmetic Manfred <noname@add.invalid> - 2021-12-17 16:24 +0100
      Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-17 18:17 +0100
    Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:11 -0300

csiph-web