functions that manipulate void pointers


 
Thread Tools Search this Thread
Top Forums Programming functions that manipulate void pointers
# 1  
Old 12-07-2009
functions that manipulate void pointers

I have two or more linked lists that have the same names for their "next". For example,

struct server_t {
sockaddr_in * sin;
server_t * next_;
}

struct player_t {
char name[10];
player_t * next_;
}

How can I get a function to take in either type and manipulate the pointers?

I tried something like this:

static void add_entity( void * list, void * new) {
new->next_ = list;
list = new;
}

I thought that maybe I should cast add_entity() using a function pointer:

typedef void (*player_handler_t)(player_t * list, player_t * target);
typedef void (*server_handler_t)(server_t * list, server_t * target);

I was wondering what the most optimal method is?
# 2  
Old 12-07-2009
IF I understand - one uses void * as the datatype in function arguments so that it can be cast to a fixed (read: one) datatype in the function.

If you choose to use anonymous blocks in the function, then you could use if() or switch() to select a datatype depending on another variable.

The best choice is to stick with a single datatype. If you need something "cool" use function pointers to branch to the right choice for the datatype you want:

The Function Pointer Tutorials - Syntax
# 3  
Old 12-08-2009
MySQL functions that manipulate void pointers

technically you cannot do deferencing on void pointer,

its better to add one more argument to function which is either a enum or integer, which could be used to identify the data type.


Best Regards,
Rakesh UV
# 4  
Old 12-08-2009
You could go the way you're going with function pointers, but you may want to make the function prototype take two void * as arguments. While it isn't exactly type safe, it does allow you to call the function pointer without warnings regarding the types passed. If you don't do this, all your callers will need to know which of the two functions they are calling to pass the correct type safe parameters.

so something like:

Code:
struct server_t
{
     sockaddr_in * sin;
     server_t * next_;
}

struct player_t
{
     char name[10];
     player_t * next_;
}

typedef void (*add_entity_t)(void **list, void * target);

static void add_server_entity(void **_list, void *_new)
{
     #define list   ((struct server_t *) _list)
     #define new    ((struct server_t *) _new)

     new->next_ = *list;
     *list = new;

     #undef list
     #undef new
}


static void add_player_entity(void **_list, void *_new)
{
     #define list   ((struct player_t *) _list)
     #define new    ((struct player_t *) _new)

     new->next_ = *list;
     *list = new;

     #undef list
     #undef new
}

int main(int argc, char **argv)
{
     add_entity_t    entityFn;

     ....

    entityFn = add_server_entity;

    entityFn(&serverHead, newServer);
}

I fixed the "list" argument, which needs to be a pointer to pointer since you are changing its value and the caller needs to see the new value for everything to work.

However, to be honest, I think all this is just a "waste" of time. You should concentrate on making a list package that is generic. Like, add_entity just takes a list and a void * for the data. It allocates nodes appropriately and you allocate the data you want to add to the list outside and pass it to an "add to list" function. The list entity just stores the next/prev pointers and a void * for the data item. In C++ you could use templates, in C you have to sacrifice type safety for the generic data item.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Malloc to void pointer fails

I have a function to which I will pass a struct ID and it will return me a string. I will pass a pointer to store the name string and that pointer will be allocated memory by the function called. int ConvertIDToName(void *id, void *name, size_t *size) { int status = 0; ... (5 Replies)
Discussion started by: rupeshkp728
5 Replies

2. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

3. Programming

void pointer

hi guys! Is there such a thing as double void pointer dynamic allocation? And if so is it something like this? int n; void** a; a=malloc(n*sizeof(void*)); (12 Replies)
Discussion started by: vlm
12 Replies

4. Programming

Using pointers to struct members as args to functions

In a well-known book on the C language, there is an example of an efficient method for using a struct member as an argument to a function. (I'm a C noob, but I believe the correct terminology might be: use call-by-reference instead of call-by-value.) The function is printf. Anyway, here's a... (5 Replies)
Discussion started by: uiop44
5 Replies

5. Homework & Coursework Questions

C++ struct pointers & functions

Hi All, My latest assignment (practice not coursework!) is to write prototype interactive exam/test console application. I've used structs to store the question information (not sure if this was the best way to do it?) and I have the following code that outputs each question and it's possible... (0 Replies)
Discussion started by: pondlife
0 Replies

6. UNIX for Dummies Questions & Answers

void (char *asd)

void asdf(char *asd) is this thing a pointer? (1 Reply)
Discussion started by: khestoi
1 Replies

7. Programming

What is the difference between f(...), f(void) and f()

What is the difference between f(...) , f(void),f() I know that f(void) doesn't take any parameters, but what about f() and f(...) Does the last call of function even exists? (2 Replies)
Discussion started by: purplelightspar
2 Replies

8. Programming

How to return void function pointer

Hello all im trying to build function that will return void function pointer what is mean is ( not working ) the main function void * myClass::getFunction(int type){ if(type==1) return &myClass::Test1; if(type==2) return &myClass::Test2; } void myClass::Test1(){... (1 Reply)
Discussion started by: umen
1 Replies

9. Shell Programming and Scripting

Sorting the Void File in CSH

First and foremost, this is not a homework for your information. I'm just new to using c-shell programming and I just wanted to make my life easier @ work. Say, the file contains the following: ID FILE NO. SL VP 1 1 22 33 1 2 ... (3 Replies)
Discussion started by: ilak1008
3 Replies
Login or Register to Ask a Question