Thread: ascii to pointer?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    5

    ascii to pointer?

    I'm recalling the gurus of C: is any way of converting from a string that contains the address of a pointer to the pointer? Everything would be fine if "atoh()" would exist =_=.

    void * mail;
    mail = map_mem(ipc_mail);
    int * arr_mail;
    arr_mail = atoh(mail*); <--- ??!?!?!
    mail contains a (char *) within is stored the value of a pointer in ascii like this :

    int arr_mail[9];
    void * mail;
    mail = map_mem(ipc_mail);
    sprintf(mail,"&#37;p",arr_mail);
    PS: Sorry about my english and thanks to everone

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    sscanf(mail, %p", &ptr) perhaps? [I haven't tried it].

    Otherwise you should be able to use "%x" or "%lx" - pointers are normally convertable to long integers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    Oh, fast reply! Thanks!!

    I will try it out

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Of course, this sort of begs the question "Why the **** are you doing this?"

    There is really no good reason to convert a pointer to a string and then to convert it back to a pointer again - pointers are generally only valid within their own process, so there's no reason to make pointer to text so that you can send them across a network, which is the most common reason to convert a binary value to text.

    So whilst my previous post may have given you a solution, the question I should have asked in the response should have been "Are you sure you want to do that?".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    int arr_mail[9];
    mail = map_mem(ipc_mail);
    sscanf(*mail,"&#37;p",arr_mail);
    Compier says:
    warning: passing arg 1 of `sscanf' makes pointer from integer without a cast
    Damn, that was close >_<



    Answering on your question: I am doing a multi-process program and i MUST use the (map_mem) function but I don't want to make the execlp() with 9 parameters, and later use it 9 times for the array that i have. So i make an IPC i give the array to the IPC and i only have the "execlp" with 1 parameter.

    (I'm not used to computer language at english so if you don't understand what i'm trying to say, ask, and i will try to explain it the best i can :/)
    Last edited by Khanser; 01-02-2008 at 08:44 AM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should not have a star on the first argument to sscanf(). That is the pointer to the string you are reading your data from.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    without the star:
    warning: passing arg 1 of `sscanf' from incompatible pointer type
    Well the pointer at string format is within the pointer so i think the star would be at good place right?
    (I'm a baddass n0b with pointers)

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ah, yes, your mail is a void pointer, isn't it. Casting it like this should work:
    Code:
    sscanf((char *)mail, ...)
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    Lol, thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  4. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 09:40 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM