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


Groups > comp.lang.c > #89100

Re: *&aptr , &*aptr question

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: *&aptr , &*aptr question
Date 2016-06-14 14:37 -0700
Organization None to speak of
Message-ID <lnr3bz1mfl.fsf@kst-u.example.com> (permalink)
References <9e50cd7d-da7b-4f90-9c83-57799cd07ad6@googlegroups.com>

Show all headers | View raw


G G <gdotone@gmail.com> writes:
> refer to pointer:
>    & and * operators are compliments of each other
>
> below is a snip from C How to Program
> ...
>
> int a;
> int *aPtr;
>
> a = 7;
> aPtr = &a;
>
> let's say the address is of a 0028FEC0

Ok.  Note that that's probably verbal shorthand for something like:

    The address of a, when converted to an appropriate integer type,
    is 0x0028FEC0.

Or, to put it another way:

    &a == (int*)0x0028FEC0

(I'm still making some assumptions about pointer/integer conversions,
assumptions that are probably only about 99% reliable.)

Addresses (pointer values) are not numbers.

(I'm being pedantic; I do know what you mean.)

> so the address is of a is 0028FEC0
>      the value of aPtr is 0028FEC0

Right.

> how does &*aPtr resolve?

`aPtr` is of type `int*`.  `*aPtr` refers to the object that `aPtr`
points to; that object is of type `int`.  `&*aPtr` yields the address of
that `int` object; that address is of type `int*`.

> *aPtr has the value of 7  or points to the a which has a value of 7

`*aPtr` isn't a pointer, so it doesn't point to anything.

The expression `*aPtr` is an *lvalue*.  The expression `a` is also an
lvalue.  An lvalue is an expression that can designate an object.

If you evaluate an lvalue in a context that doesn't require an lvalue,
the result is the value of the designated object.  If you evaluate an
lvalue in a context that *does* require an lvalue, the value of the
object is not used.  The operand of unary `&` is one such context.

> so, is & then applied to the 7. &7, and the address where 7 is located
> returned. if that's the case, then where ever a pointer points and
> then the value at that location is dereference the address is returned?

No, `&7` is not legal, because `7` is not an lvalue.  What's relevant to
the `&` operator is the *object* that its operand refers to.  The value
of that object (which happens to be 7 in this case) is ignored.

> how does *&aPtr resolve?

`aPtr` is an object of type `int*`.  `&aPtr` yields that object's
address, which is of type `int**`.  Applying `*` to that value gives you
the original object `aPtr`.  `*&aPtr` is an lvalue.

> &Ptr gives the address of Ptr   which let's say is 0028FFF0
> so *0028FFF0 returns the value stored there, which is 0028FECO
>
> am i thinking about this correctly?

Mostly.

There's one more special rule.  If you write `&*foo`, neither the `&`
nor the `*` is evaluated, and the expression is equivalent to just
`foo` (except that it's not an lvalue).  Similarly, `&foo[i]` is
equivalent to `foo + i` (since `arr[i]` in general is by definition
equivalent to `*(arr+i)`).

-- 
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

*&aptr , &*aptr question G G <gdotone@gmail.com> - 2016-06-14 13:43 -0700
  Re: *&aptr , &*aptr question Richard Heathfield <rjh@cpax.org.uk> - 2016-06-14 22:01 +0100
    Re: *&aptr , &*aptr question Ike Naar <ike@iceland.freeshell.org> - 2016-06-14 21:20 +0000
      Re: *&aptr , &*aptr question G G <gdotone@gmail.com> - 2016-06-14 14:38 -0700
    Re: *&aptr , &*aptr question Richard Heathfield <rjh@cpax.org.uk> - 2016-06-14 22:42 +0100
    Re: *&aptr , &*aptr question Ken Brody <kenbrody@spamcop.net> - 2016-06-15 14:22 -0400
      Re: *&aptr , &*aptr question Richard Heathfield <rjh@cpax.org.uk> - 2016-06-15 19:24 +0100
  Re: *&aptr , &*aptr question "James R. Kuyper" <jameskuyper@verizon.net> - 2016-06-14 17:17 -0400
  Re: *&aptr , &*aptr question Keith Thompson <kst-u@mib.org> - 2016-06-14 14:37 -0700
  Re: *&aptr , &*aptr question "James R. Kuyper" <jameskuyper@verizon.net> - 2016-06-14 17:40 -0400
    Re: *&aptr , &*aptr question "James R. Kuyper" <jameskuyper@verizon.net> - 2016-06-14 17:49 -0400
    Re: *&aptr , &*aptr question BartC <bc@freeuk.com> - 2016-06-14 22:57 +0100
      Re: *&aptr , &*aptr question jameskuyper@verizon.net - 2016-06-14 15:22 -0700
        Re: *&aptr , &*aptr question "James R. Kuyper" <jameskuyper@verizon.net> - 2016-06-14 18:40 -0400
        Re: *&aptr , &*aptr question BartC <bc@freeuk.com> - 2016-06-14 23:44 +0100
        Re: *&aptr , &*aptr question BartC <bc@freeuk.com> - 2016-06-14 23:54 +0100
          Re: *&aptr , &*aptr question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-06-15 00:14 +0100
          Re: *&aptr , &*aptr question Keith Thompson <kst-u@mib.org> - 2016-06-14 16:51 -0700
            Re: *&aptr , &*aptr question BartC <bc@freeuk.com> - 2016-06-15 01:15 +0100
              Re: *&aptr , &*aptr question Keith Thompson <kst-u@mib.org> - 2016-06-14 17:44 -0700
              Re: *&aptr , &*aptr question David Brown <david.brown@hesbynett.no> - 2016-06-15 09:12 +0200
                Re: *&aptr , &*aptr question Richard Heathfield <rjh@cpax.org.uk> - 2016-06-15 08:28 +0100
                Re: *&aptr , &*aptr question David Brown <david.brown@hesbynett.no> - 2016-06-15 10:26 +0200
                Re: *&aptr , &*aptr question Richard Heathfield <rjh@cpax.org.uk> - 2016-06-15 09:48 +0100
                Re: *&aptr , &*aptr question BartC <bc@freeuk.com> - 2016-06-15 11:05 +0100
                Re: *&aptr , &*aptr question David Brown <david.brown@hesbynett.no> - 2016-06-15 12:22 +0200
                Re: *&aptr , &*aptr question Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-06-15 03:55 -0700
                Re: *&aptr , &*aptr question David Brown <david.brown@hesbynett.no> - 2016-06-15 13:15 +0200
                Re: *&aptr , &*aptr question BartC <bc@freeuk.com> - 2016-06-15 13:08 +0100
                Re: *&aptr , &*aptr question David Brown <david.brown@hesbynett.no> - 2016-06-15 14:15 +0200
                Re: *&aptr , &*aptr question BartC <bc@freeuk.com> - 2016-06-15 14:02 +0100
                Re: *&aptr , &*aptr question Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-06-15 13:38 -0700
                Re: *&aptr , &*aptr question David Brown <david.brown@hesbynett.no> - 2016-06-16 00:01 +0200
                Re: *&aptr , &*aptr question Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-06-16 00:58 -0700
                Re: *&aptr , &*aptr question supercat@casperkitty.com - 2016-06-15 09:06 -0700
                Re: *&aptr , &*aptr question BartC <bc@freeuk.com> - 2016-06-15 17:54 +0100
                Re: *&aptr , &*aptr question raltbos@xs4all.nl (Richard Bos) - 2016-06-15 20:10 +0000
    Re: *&aptr , &*aptr question supercat@casperkitty.com - 2016-06-14 16:28 -0700
      Re: *&aptr , &*aptr question Keith Thompson <kst-u@mib.org> - 2016-06-14 16:54 -0700
        Re: *&aptr , &*aptr question supercat@casperkitty.com - 2016-06-14 19:13 -0700
          Re: *&aptr , &*aptr question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-06-15 13:19 +0100
            Re: *&aptr , &*aptr question Tim Rentsch <txr@alumni.caltech.edu> - 2016-06-16 10:39 -0700
              Re: *&aptr , &*aptr question supercat@casperkitty.com - 2016-06-16 12:06 -0700
                Re: *&aptr , &*aptr question Tim Rentsch <txr@alumni.caltech.edu> - 2016-06-21 12:21 -0700
              Re: *&aptr , &*aptr question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-06-16 21:06 +0100
                Re: *&aptr , &*aptr question supercat@casperkitty.com - 2016-06-16 14:35 -0700
                Re: *&aptr , &*aptr question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-06-17 01:22 +0100
                Re: *&aptr , &*aptr question Tim Rentsch <txr@alumni.caltech.edu> - 2016-06-16 22:21 -0700
                Re: *&aptr , &*aptr question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-06-18 00:55 +0100
                Re: *&aptr , &*aptr question supercat@casperkitty.com - 2016-06-17 17:26 -0700
                Re: *&aptr , &*aptr question Tim Rentsch <txr@alumni.caltech.edu> - 2016-06-21 12:19 -0700
                Re: *&aptr , &*aptr question supercat@casperkitty.com - 2016-06-21 13:09 -0700
  Re: *&aptr , &*aptr question Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-14 22:30 -0400
    Re: *&aptr , &*aptr question Tim Rentsch <txr@alumni.caltech.edu> - 2016-06-15 14:20 -0700

csiph-web