Fedora Linux Support Community & Resources Center
  #1  
Old 21st November 2006, 05:32 AM
FuerzaDelDios Offline
Registered User
 
Join Date: Nov 2006
Posts: 38
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
Reply With Quote
  #2  
Old 22nd November 2006, 03:12 PM
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
It's mainly used in function prototypes to indicate a pointer whose type is only known at runtime.
Reply With Quote
  #3  
Old 23rd November 2006, 04:30 AM
FuerzaDelDios Offline
Registered User
 
Join Date: Nov 2006
Posts: 38
Thanks guilix.
Reply With Quote
  #4  
Old 29th November 2006, 04:06 PM
theconley Offline
Registered User
 
Join Date: Sep 2006
Location: Roanoke, VA
Age: 27
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
  #5  
Old 30th November 2006, 08: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:

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 09:31 PM.
Reply With Quote
Reply

Tags
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 12:17 AM
Mouse pointers whitebeard Using Fedora 3 12th February 2009 09: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 10:30 PM
Mismatched Pointers??? budds Servers & Networking 5 6th October 2004 08:28 AM


Current GMT-time: 21:10 (Friday, 18-04-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

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