void pointer to pointer

This is a discussion on void pointer to pointer within the C Programming forums, part of the General Programming Boards category; Let's say we have a function func that that takes a void ** argument and changes what *arg points to. ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    399

    void pointer to pointer

    Let's say we have a function func that that takes a void ** argument and changes what *arg points to.
    Code:
    void func(void **arg);
    I have to cast the argument to void ** so the compiler doesn't complain, which I'm guessing is a sign that I'm doing something that I shouldn't be doing.
    Code:
    int *ptr;
    func((void **)&ptr);

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It doesn't look like you are doing anything wrong. Is there an actual problem with your code? Most likely you are using some C++ compiler which requires explicit casting.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    or perhaps you forgot to #include stdlib.h?

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    I'm guessing it has something to do with this:

    Chapter 22: Pointers to Pointers

    One side point about pointers to pointers and memory allocation: although the void * type, as returned by malloc, is a ``generic pointer,'' suitable for assigning to or from pointers of any type, the hypothetical type void ** is not a ``generic pointer to pointer.''

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094

    Thumbs up

    Quote Originally Posted by Memloop View Post
    I'm guessing it has something to do with this:

    Chapter 22: Pointers to Pointers
    Good find!

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Pointer types can only convert to void * without a cast. void ** is not the same as void *. You can relieve the caller of the need to cast by doing it yourself:

    Code:
    void func(void *arg)
    {
        void **realarg = (void **)arg;
        /* whatever */
    }
    EDIT: To expand a bit, "void *" means "a pointer to anything," which is very general. "void **" means "a pointer to a void *" which is much more specific.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 02:58 PM
  2. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 08:24 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21