Home > Net >  How to take a pointer address as command line argument in C
How to take a pointer address as command line argument in C

Time:09-07

I have a C code which will take a pointer address as an argument. The code arguments are:

./main 0x7fad529d5000

Now when reading the arguments, this value will be read as a string.

How do I convert the string "0x7fad529d5000" into an address?

CodePudding user response:

Read a hexadecimal:

long x;
std::cin >> std::hex >> x;

Converting it to a pointer:

void * p = reinterpret_cast<void *>(x);

Handle that pointer value with care, because it is unlikely to be valid. (reinterpret_cast is something that should scream "danger!" at you.)

CodePudding user response:

One option is to use void* as shown below:

void func( void *param )
{
    //use param here
}
int main()
{
    func(/* pass address here*/);
}
  • Related