FedoraForum.org - Fedora Support Forums and Community
Results 1 to 5 of 5
  1. #1
    FuerzaDelDios Guest

    Use of void pointers in C

    Hi,

    A void pointer in C cannot b derefrenced, nor can any arithmetic operation be performed on it. So what is a void pointer used for..??

    Regs
    FDD

  2. #2
    Join Date
    Oct 2005
    Location
    GMT
    Posts
    3,242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's mainly used in function prototypes to indicate a pointer whose type is only known at runtime.
    Asus K55VD, i5 3230M - Productivity/Programming (F28)
    Asus M32CD - i7-6700, Asus STRIX-GTX970-DC2OC-4GD, 1x8 GB Sk Hynix 2133 MHz DDR4 - Gaming (W10)

  3. #3
    FuerzaDelDios Guest
    Thanks guilix.

  4. #4
    theconley Guest
    It can also be used to store a bunch of stuff that you don't know what it is. Example:

    If you want to create a process thread, using the pthread library, you need to pass it a void* to all the resources you want to use.

  5. #5
    gandalf013 Guest
    void pointers are very useful. Let's look at the standard library function malloc(). It needs to return a pointer to an appropriately aligned and containing the required amount of storage. One can call malloc() to allocate space for any type, including user defined types:

    Code:
    struct t {
    
    char *s;
    }; ... struct t x; ... x = malloc(sizeof *x);
    So, what should be return type of malloc()? The answer is void *, because a void * can be assigned to any other pointer type without any loss of information. Another example is the function qsort(), which can be used to sort any type of variables in an array. For this, qsort() expects a function that can compare the types in question. Now, since the types can be anything, the function prototype is supplied as:

    Code:
    int(*compar)(const void *, const void *);
    So, one could write a trivial function to sort an array of ints:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int cmp_int(const void *a, const void *b);
    
    int main(void)
    {
    
    int a[] = { 1, 5, 2, 9, 3}; size_t n = sizeof a / sizeof a[0]; qsort(&a, n, sizeof a[0], cmp_int); for (i=0; i < n; i++)
    printf("%d\n", a[i]);
    return EXIT_SUCCESS;
    } int cmp_int(const void *a, const void *b) {
    const int *f = a; const int *s = b; if (*f < *s) {
    return -1;
    } else if (*f > *s) {
    return 1;
    } else {
    return 0;
    }
    }
    Note the first two assignment statements in cmp_int().

    void * was not present in K&R C. In K&R C, one had to use char * to the same thing. I think even today using char * in place of void * is guaranteed to work by the standard, but I am not sure. In any case, using void * makes more sense anyway as it is less confusing.

    HTH
    Last edited by gandalf013; 6th December 2006 at 10:31 PM.

Similar Threads

  1. kde mouse pointers
    By whitebeard in forum Using Fedora
    Replies: 3
    Last Post: 22nd February 2009, 01:17 AM
  2. Mouse pointers
    By whitebeard in forum Using Fedora
    Replies: 3
    Last Post: 12th February 2009, 10:42 PM
  3. Some migration pointers please :)
    By dgrafix in forum Using Fedora
    Replies: 6
    Last Post: 21st May 2008, 10:34 AM
  4. "Fedora in a Void" install
    By Dubious Dave in forum Fedora Focus
    Replies: 19
    Last Post: 17th December 2007, 11:30 PM
  5. Mismatched Pointers???
    By budds in forum Servers & Networking
    Replies: 5
    Last Post: 6th October 2004, 08:28 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •