Thread: Dereferncing Void Pointer

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    11

    Dereferncing Void Pointer

    Hi, so that I could understand void pointers I created the following program:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
    
        char word[] = "Zero";
        int number = 0;
        void *ptr = NULL;
    
        printf("number = %d\t[%p]\n",number,&number);
        printf("word   = %s\t[%p]\n",word,&word);
    
        /* Point to number. */
        ptr = &number;
    
        printf("*ptr   = %d\t[%p]\n",*((int *)ptr),(int *)ptr);
    
        /* Modify value of number. */
        *((int *)ptr) = 1;
    
    
        /* Point to word. */
        ptr = &word;
    
        printf("*ptr   = %s\t[%p]\n",(char **)ptr,(char *)ptr);
    
        /* Modify value of product */
        strcpy(ptr,"One");
    
        printf("number = %d\t[%p]\n",number,&number);
        printf("word   = %s\t[%p]\n",word,&word);
    
        return 0;
    
    }

    The program works fine, however i really want to fully understand what is going on with the deferencing of the void pointer, for example:

    With the following code:

    Code:
        ptr = &number; 
        *((int *)ptr) = 1;
    Why can't you just do:

    Code:
        ptr = &number;    
        *(int *)ptr = 1;
    And again with this code, (i'm guessing it's becuase its a pointer to a pointer?):

    Code:
        ptr = &word;
        strcpy(ptr,"One");
    Could someone please explain why this is neccessary or if you can point me at
    an explaination I would be most grateful for any assistace.

    Thanks
    ~P

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    To dereference a pointer, it is necessary for the compiler to know the type of whatever it points at. A void pointer - by definition - is a pointer for which the compiler doesn't know what it points at. The type conversion is necessary to tell the compiler what the pointer points at.

    The additional brackets in your example are just for clarity.

    The strcpy() call will work, since a void pointer can be converted implicitly to any other pointer, and vice versa, in C. Since (if <string.h> is #include'd) the compiler knows that strcpy() accepts a const char *, it can do the conversion from void * to const char *.

    Incidentally, &word should not be passed to strcpy() .... word should be (as it will be implicitly converted to a pointer to char, equal to &word[0]). &word has type "pointer to array of five characters", whereas word (when converted to a pointer) will have type "pointer to char". The thing is, those two pointers have the same value, but different type,so strcpy(&word, "One") won't compile. The usage of a void pointer discards the type info and, since the values are the same, the strcpy() call works. All that makes for really shonky technique in C.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    Hi, it was the additional brackets that I had issue with, earlier in the program, I had compilation errors without them, clearly I must have done something wrong.

    Thanks for your assistance.

    ~P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting to pointer to pointer to void
    By Sharke in forum C Programming
    Replies: 13
    Last Post: 05-12-2009, 08:40 PM
  2. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  3. Void Pointer and Pointer to Void
    By vb.bajpai in forum C Programming
    Replies: 13
    Last Post: 06-14-2007, 06:17 PM
  4. Dereference pointer to void pointer to member
    By phil in forum C Programming
    Replies: 5
    Last Post: 04-20-2005, 11:54 AM
  5. Need help on pointer to VOID
    By dv007 in forum C Programming
    Replies: 19
    Last Post: 07-09-2002, 06:15 PM