linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Manish Katiyar <mkatiyar@gmail.com>
To: Aneesh Bhasin <contact.aneesh@gmail.com>
Cc: Saurabh Sehgal <saurabh.r.s@gmail.com>,
	linux-c-programming@vger.kernel.org
Subject: Re: memory address represented as a string
Date: Sun, 26 Jul 2009 13:36:38 +0530	[thread overview]
Message-ID: <ea11fea30907260106j74614d49j2ece27b9c05d9a40@mail.gmail.com> (raw)
In-Reply-To: <f662f0210907260059l36076e16rb29f75640445bc7a@mail.gmail.com>

On Sun, Jul 26, 2009 at 1:29 PM, Aneesh Bhasin<contact.aneesh@gmail.com> wrote:
> Hi Saurabh,
>
> On Sun, Jul 26, 2009 at 1:09 PM, Saurabh Sehgal <saurabh.r.s@gmail.com> wrote:
>>
>> Hi all,
>>
>> I had a quick question:
>>
>> Let's say I design a function with the signature:
>>
>> void * foo( char * addr ) ; ,
>>
>> where addr is a string that represents a valid memory address  ...
>> so the way someone can call this function is ...
>>
>> char * addr = "0xae456778" // assume this is a valid memory address on
>> the machine
>
> This means that addr points to a memory location where the string
> stored is "0xae456778"
>
>
>>
>> foo( addr ) ;
>>
>> Is it possible to take this address in string form, and assign it to
>> an actual pointer of void * type ?

/tmp> cat test.c
#include<stdio.h>

int *addr(char *address)
{
  int ptr;
  ptr=strtoul(address, 0 , 16);
  if (ptr==-1)
    {
      perror("strtoul failed:");
    }
  return (int *)ptr;
}

int *foo()
{
  char c[10];
  int *ptr=malloc(sizeof(int));
  sprintf(c,"%p",ptr);
  *ptr=100;
  return addr(c);

}

int main()
{
  int *p=foo();
  printf("%d\n",*p);
  return 0;
}
/tmp> gcc test.c
test.c: In function ‘foo’:
test.c:17: warning: incompatible implicit declaration of built-in
function ‘malloc’
/tmp> ./a.out
100

Thanks -
Manish


>
> Yes, you can parse each individual character of the hex-format string
> and convert it to its integral equivalent (you can find many samples
> of this on internet...) and assing this value to a void * and then
> return it back..
>
> Hope that helps..
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>



-- 
Thanks -
Manish
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  reply	other threads:[~2009-07-26  8:06 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-26  7:39 memory address represented as a string Saurabh Sehgal
2009-07-26  7:59 ` Aneesh Bhasin
2009-07-26  8:06   ` Manish Katiyar [this message]
     [not found] ` <b21328ed0907260114w547eb3fax61927111cdcf9d3c@mail.gmail.com>
2009-07-26  8:26   ` Saurabh Sehgal
2009-07-28 10:09     ` Rahul K Patel
2009-07-26 16:23 ` Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ea11fea30907260106j74614d49j2ece27b9c05d9a40@mail.gmail.com \
    --to=mkatiyar@gmail.com \
    --cc=contact.aneesh@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=saurabh.r.s@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).