Sponsored Content
Top Forums Programming functions that manipulate void pointers Post 302378647 by DreamWarrior on Tuesday 8th of December 2009 11:24:30 AM
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.
 

9 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

9. 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
insque(3C)						   Standard C Library Functions 						insque(3C)

NAME
insque, remque - insert/remove element from a queue SYNOPSIS
include <search.h> void insque(struct qelem *elem, struct qelem *pred); void remque(struct qelem *elem); DESCRIPTION
The insque() and remque() functions manipulate queues built from doubly linked lists. Each element in the queue must be in the following form: struct qelem { struct qelem *q_forw; struct qelem *q_back; char q_data[]; }; The insque() function inserts elem in a queue immediately after pred. The remque() function removes an entry elem from a queue. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |Unsafe | +-----------------------------+-----------------------------+ SEE ALSO
attributes(5), standards(5) SunOS 5.11 24 Jul 2002 insque(3C)
All times are GMT -4. The time now is 02:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy