Skip to content

Instantly share code, notes, and snippets.

@Apsu
Created October 12, 2012 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Apsu/3877250 to your computer and use it in GitHub Desktop.
Save Apsu/3877250 to your computer and use it in GitHub Desktop.
Void pointers
#include <stdio.h>
struct data {
int fd;
};
void test(void *d) {
struct data *x = (struct data *)d; // Cast the other way
printf("fd = %d\n", x->fd); // Dereference
}
int main(int argc, char **argv) {
struct data x; // Stack-local 'data' struct
void *p = (void *)&x; // Cast and assign
x.fd = 1; // Assign through struct
test(p); // Pass to function
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment