Fedora Linux Support Community & Resources Center
Old 21st November 2006, 06:32 AM
FuerzaDelDios Offline
Registered User
Join Date: Nov 2006
Posts: 38
Use of void pointers in C


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..??

Reply With Quote
Old 22nd November 2006, 04:12 PM
giulix Offline
Join Date: Oct 2005
Location: CE(S)T
Posts: 3,242
It's mainly used in function prototypes to indicate a pointer whose type is only known at runtime.
Asus K55VD, i5 3230M - Productivity/Programming (F24)
Asus M32CD - i7-6700, Asus STRIX-GTX970-DC2OC-4GD, 1x8 GB Sk Hynix 2133 MHz DDR4 - Gaming (W10)
HP Proliant DL 360 G5 - Heavy lifting (CentOS 7.2)
Reply With Quote
Old 23rd November 2006, 05:30 AM
FuerzaDelDios Offline
Registered User
Join Date: Nov 2006
Posts: 38
Thanks guilix.
Reply With Quote
Old 29th November 2006, 05:06 PM
theconley Offline
Registered User
Join Date: Sep 2006
Location: Roanoke, VA
Age: 30
Posts: 32
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.
Reply With Quote
Old 30th November 2006, 09:11 PM
gandalf013 Offline
Registered User
Join Date: May 2006
Posts: 5
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:

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:

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

#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]);
} 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.


Last edited by gandalf013; 6th December 2006 at 10:31 PM.
Reply With Quote

pointers, void

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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

Current GMT-time: 04:05 (Sunday, 20-08-2017)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat