Thread: working with void ** (NOOB)

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    9

    Question working with void ** (NOOB)

    I'm a total beginner... so please go easy on me

    i have a function that gets some parameters, i'm interested in two of them.
    one is void **args
    and one is int nargs

    The first is probably an array, and the second one is the number of items in the array.
    assume that I know that all the items in the array are int.

    how can I access their values?

    Thanks

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I suppose you have to revert all the steps that it took to call the function with the original data.

    For example:
    Code:
    #include <cstdio>
    
    void foo(int count, void** arr)
    {
         void* vp = *arr;    //dereference to obtain void pointer
         int* p = (int*)vp;  //and cast it back to int pointer
         for (int i = 0; i != count; ++i) {
             printf("%d ", p[i]);
         }
    }
    
    int main()
    {
        int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        void* p = (void*)arr; //cast to void*
        foo(10, &p);          //and pass its address
    }
    But why would you mess with the void pointers if you know they point to an int array?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix operations using objects
    By circuitbreaker in forum C++ Programming
    Replies: 7
    Last Post: 05-04-2010, 08:53 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM