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


Groups > comp.lang.c > #89294

Re: how does one access a specific address location in C?

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: how does one access a specific address location in C?
Date 2016-06-16 17:03 +0100
Organization A noiseless patient Spider
Message-ID <87shwdunmh.fsf@bsb.me.uk> (permalink)
References <994e7598-1570-4931-8cee-d5a7abb967a5@googlegroups.com> <mstorkamp-017076.09162116062016@88-209-239-213.giganet.hu> <482fe8e7-a95e-4579-8baf-90565a1530e9@googlegroups.com> <1a8af5ff-56b1-42a5-972f-20e44e573c63@googlegroups.com>

Show all headers | View raw


G G <gdotone@gmail.com> writes:

> On Thursday, June 16, 2016 at 10:50:26 AM UTC-4, G G wrote:
<snip>
>> #include <stdio.h>
>> 
>> int main( void )
>> {
>>    int a;
>>    int *aPtr;
>> 
>>    a = 7;
>>    aPtr = &a;
>> 
>>    printf( "The address of a is %p"
>>            "\nThe value of aPtr is %p", &a, aPtr );

Technically, you need to cast these pointers to void *.  %p expects a
void * but you probably won't ever use a machine where this matters.

>>    printf( "\n\nThe value of a is %d"
>>            "\nThe value of *aPtr is %d", a, *aPtr );
>> 
>>    printf( "\n\nShowing that * and & are complements of "
>>            "each other\n&*Ptr = %p"
>>            "\n*&aPtr = %p\n", &*aPtr, *&aPtr );

Ditto.

>>    unsigned int *peekAtValuePtr = (unsigned int *) 0x7fff5596ec1c; //
>>    address of a on my machine

Why unsigned int?  Later you want an int.  It may not matter much in
this case but why mess about with the types?

>>    printf( "The value at this location in memory is %d", (int) *peekAtValuePtr );

There is no need for this cast.

>> } // end main
>> 
>> that does give me the value at that address. 
>> 
>
> It gives me a segmentation fault:  11

When I run this program, the address of a changes each time so I can't
"hard wire" it into the program.  Are you sure you are not getting the
same effect?  It would help if you posted the output.

<snip>
-- 
Ben.

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


Thread

how does one access a specific address location in C? G G <gdotone@gmail.com> - 2016-06-16 06:32 -0700
  Re: how does one access a specific address location in C? gazelle@shell.xmission.com (Kenny McCormack) - 2016-06-16 14:00 +0000
  Re: how does one access a specific address location in C? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-06-16 15:07 +0100
    Re: how does one access a specific address location in C? G G <gdotone@gmail.com> - 2016-06-16 07:18 -0700
      Re: how does one access a specific address location in C? Robert Wessel <robertwessel2@yahoo.com> - 2016-06-16 11:38 -0500
  Re: how does one access a specific address location in C? Mark Storkamp <mstorkamp@yahoo.com> - 2016-06-16 09:16 -0500
    Re: how does one access a specific address location in C? G G <gdotone@gmail.com> - 2016-06-16 07:50 -0700
      Re: how does one access a specific address location in C? G G <gdotone@gmail.com> - 2016-06-16 08:04 -0700
        Re: how does one access a specific address location in C? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-06-16 17:03 +0100
          Re: how does one access a specific address location in C? G G <gdotone@gmail.com> - 2016-06-16 10:01 -0700
            Re: how does one access a specific address location in C? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-16 14:29 -0400
            Re: how does one access a specific address location in C? Mark Storkamp <mstorkamp@yahoo.com> - 2016-06-16 13:34 -0500
              Re: how does one access a specific address location in C? BartC <bc@freeuk.com> - 2016-06-16 19:57 +0100
                Re: how does one access a specific address location in C? raltbos@xs4all.nl (Richard Bos) - 2016-06-18 20:50 +0000
                Re: how does one access a specific address location in C? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-18 17:23 -0400
                Re: how does one access a specific address location in C? raltbos@xs4all.nl (Richard Bos) - 2016-06-18 21:30 +0000
            Re: how does one access a specific address location in C? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-06-16 21:09 +0100
        Re: how does one access a specific address location in C? Mark Storkamp <mstorkamp@yahoo.com> - 2016-06-16 11:10 -0500
      Re: how does one access a specific address location in C? James Kuyper <jameskuyper@verizon.net> - 2016-06-16 13:41 -0400
        Re: how does one access a specific address location in C? G G <gdotone@gmail.com> - 2016-06-16 11:14 -0700
          Re: how does one access a specific address location in C? Keith Thompson <kst-u@mib.org> - 2016-06-16 11:26 -0700
          Re: how does one access a specific address location in C? James Kuyper <jameskuyper@verizon.net> - 2016-06-16 16:38 -0400
      Re: how does one access a specific address location in C? "James R. Kuyper" <jameskuyper@verizon.net> - 2016-06-18 11:32 -0400
  Re: how does one access a specific address location in C? James Kuyper <jameskuyper@verizon.net> - 2016-06-16 12:45 -0400
    Re: how does one access a specific address location in C? G G <gdotone@gmail.com> - 2016-06-16 10:19 -0700
    Re: how does one access a specific address location in C? Keith Thompson <kst-u@mib.org> - 2016-06-16 11:20 -0700
    Re: how does one access a specific address location in C? supercat@casperkitty.com - 2016-06-16 11:38 -0700
      Re: how does one access a specific address location in C? raltbos@xs4all.nl (Richard Bos) - 2016-06-18 13:19 +0000
        Re: how does one access a specific address location in C? supercat@casperkitty.com - 2016-06-18 07:11 -0700
          Re: how does one access a specific address location in C? Robert Wessel <robertwessel2@yahoo.com> - 2016-06-18 14:41 -0500
            Re: how does one access a specific address location in C? supercat@casperkitty.com - 2016-06-18 16:02 -0700
              Re: how does one access a specific address location in C? Les Cargill <lcargill99@comcast.com> - 2016-06-18 18:53 -0500
                Re: how does one access a specific address location in C? supercat@casperkitty.com - 2016-06-18 17:31 -0700
                Re: how does one access a specific address location in C? Les Cargill <lcargill99@comcast.com> - 2016-06-19 06:47 -0500
                Re: how does one access a specific address location in C? Robert Wessel <robertwessel2@yahoo.com> - 2016-06-19 00:37 -0500
                Re: how does one access a specific address location in C? BartC <bc@freeuk.com> - 2016-06-19 11:07 +0100
                Re: how does one access a specific address location in C? Robert Wessel <robertwessel2@yahoo.com> - 2016-06-19 16:21 -0500
              Re: how does one access a specific address location in C? Robert Wessel <robertwessel2@yahoo.com> - 2016-06-19 00:34 -0500

csiph-web