Thread: void* pointers

  1. #1
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105

    void* pointers

    I have this mysterious code...
    Code:
    void swap(void *v[], int i, int j) {
     void *temp;
     temp = v[i];
     v[i] = v[j];
     v[j] = temp;     
    }
    What the heck does this do?
    I was suprised it even compiled.

    The function suggests something is swapped.
    If I use my logical sense, I would say this swaps what is in v[i] and v[j].
    But what I don't get is:

    how can something like this be legal:
    Code:
     temp = v[i];
    when v is a void* pointer.
    I mean..isnt it impossible to find exactly where in memory to find element nr i, when we don't know the size of the thing we're storing in our array?

    and secondly, exactly what does this do in practice?
    The book im reading suggests it "exchanges two pointers"...but this just doesent make sense to me.
    Would anyone be so kind as to try to explain it to me?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well if you replaced 'void*' with 'int', would it make sense?

    It's just swapping [i] and [j] elements of an array.
    That it's an array of void* doesn't make any difference.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    >> What the heck does this do?
    You have an array of void pointers. That code just swaps the pointer at one index with a pointer at a different index.

    >> when v is a void* pointer.
    v is not a void pointer. v is an array of void pointers.
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Ah, so this code are only swapping elements of an array of *void pointers..?

    If so, that sure helps.
    The book had just written about how void* can be used to point to all kinds of types, so I assumed for some reason that the book were showing me a universial method of swapping 2 elements in an array of whatever type.
    Which I couldnt get to make sense :P

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Drogin View Post
    I assumed for some reason that the book were showing me a universial method of swapping 2 elements in an array of whatever type.
    That is exactly what it is.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Code:
    That is exactly what it is.
    No way.

    Code:
      char array[3]; // Array of 3 chars
      
      // Set all elements to 0
      int i;
      for(i=0; i > 3; i++) {
        array[i] = 0;        
      }
    
     // Fill in some values
      array[0] = 'a';
      array[1] = 'b';
    
      swap(array,0,1);
    Are you saying this would swap array[0] and array[1], in the way I expect?
    I think not, because the function expects an array of pointers...not normal types as I first thought
    Last edited by Drogin; 09-04-2009 at 11:00 AM.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    It only works with an array of pointers -- not arrays of any type.
    bit∙hub [bit-huhb] n. A source and destination for information.

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. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM