Re: memory address represented as a string

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Jul 26, 2009 at 1:29 PM, Aneesh Bhasin<contact.aneesh@xxxxxxxxx> wrote:
> Hi Saurabh,
>
> On Sun, Jul 26, 2009 at 1:09 PM, Saurabh Sehgal <saurabh.r.s@xxxxxxxxx> 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@xxxxxxxxxxxxxxx
> 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@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux