(void *) casting

Discussion in 'C Programming' started by James Leddy, Jun 30, 2003.

  1. James Leddy

    James Leddy Guest


    I know you can cast a void pointer to an int, char, or any other type of
    pointer, but can you do the reverse, cast an int or char pointer as a void

    This is the blowfish encipher algorithim and I need to make xl and xr void
    because they come from a char * buffer, but I need them as unsigned longs.

    void encipher_dword(void *xl, void *xr)
    unsigned long *temp;
    unsigned long *al, *ar;
    int i;

    al = (unsigned long *) xl;
    ar = (unsigned long *) xr;
    for (i = 0; i < N; i++) {
    *al = *al ^ P;
    *ar = f(al) ^ *ar;
    SWAP(al, ar, temp);
    SWAP(al, ar, temp);

    *ar = *ar ^ P[N];
    *al = *al ^ P[N + 1];
    xl = (void *) al; //can I do this?
    xr = ar; //should I do it like this?

    I thought of another solution being to swap the actual values in al and ar,
    insted of just the addresses, but I thought that would be slower

    James Leddy, Jun 30, 2003
    1. Advertisements

  2. James Leddy

    Jack Klein Guest

    If you are using C, no cast is necessary for pointers. You can assign
    a pointer to void to a pointer to any other object type. You can
    assign a pointer to any object type to a pointer to void. No cast at

    If your compiler requires a cast, you are using a C++ compiler.

    On the other hand, casting a pointer type to a char or an int may
    result in undefined behavior.
    This assignment does not require a cast, unless you are using C++, in
    which case you are posting in the wrong newsgroup.

    But without seeing the code that calls this, there is no way of
    telling if xl and xr are properly aligned to point to unsigned longs.
    If not, the result is undefined behavior when you try to dereference
    them. On some platforms the system will shut down your program with
    some sort of bus error.

    If you are concerned with speed, don't settle for what you thought.
    Write a program and test it.

    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
    Jack Klein, Jul 1, 2003
    1. Advertisements

  3. Not that this was relevant to the code the OP posted, but just for
    completeness; there is (at least) one case where a cast to void * is
    necessary (or at least generally more expedient than a temporary
    variable of type void *):

    char *p;
    /* more code assigning something to p */
    printf("Pointer p: %p\n", (void *) p);

    Martien Verbruggen, Jul 5, 2003
  4. James Leddy

    Mike Wahler Guest


    Your post does not address what Jack said:

    "You can assign a pointer to void to a pointer to any
    other object type. You can assign a pointer to any
    object type to a pointer to void. No cast at all."

    Your cast is not being used with an assignment.

    Mike Wahler, Jul 5, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.