Pass void* type to main() function

155 views Asked by At

My main() should get an address as input which needs to be stored in void* address.

int main(int argc, char *argv[])
{
    if (argc > 1) {
        HandleStr = argv[1];
        printf("\n Handle passed : %s\n",HandleStr);
    }
}

I want this HandleStr as type void *. How can I do that?

Now, I want to run the exec as ./testapp "0xaf6e9800"

2

There are 2 answers

0
Serge Ballesta On

If you have a C99 compiler, you should :

  • decode argv[1] as an hexadecimal number into an intptr_t
  • assign that value to your void *

Something like :

int main(int argc, char *argv[])
{
    if (argc > 1) {
        long lh = strtol(argv[1], NULL, 16);
        if (lh != 0) {
            intptr_t ph = lh;
            void *handle = ph;
            printf("\n Handle passed : %p\n",handle);
        }
    }
}

But beware : you should know what is that value and how it should be used in your program. Because as said by Joachim Pileborg, a process cannot access any memory value.

1
Steve Summit On

I suspect you want something like

HandleStr = (void *)strtol(argv[1], NULL, 16);