| | |
Passing type as a parameter; generalised linked list in C?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I was trying to revise my linked list and implement it in C to check if there is a difference in performance... just as a sort of weird hobby..
In the absence of templates I'm trying to figure out how to pass a type as a parameter for this purpose. For example va_arg(arg, int) retrieves an int. I know it's a macro, not a function, but I still can't figure out how this is done.. if I could add this kind of functionality and then store the type somewhere in some variable then I could probably implement the linked list using void pointers.
Alternatively, would it be possible to just use void pointers and sizeof? How would one ensure that the user doesn't start mixing types within the linked list then?
Thanks for any advice, I'm really bored..
In the absence of templates I'm trying to figure out how to pass a type as a parameter for this purpose. For example va_arg(arg, int) retrieves an int. I know it's a macro, not a function, but I still can't figure out how this is done.. if I could add this kind of functionality and then store the type somewhere in some variable then I could probably implement the linked list using void pointers.
Alternatively, would it be possible to just use void pointers and sizeof? How would one ensure that the user doesn't start mixing types within the linked list then?
Thanks for any advice, I'm really bored..
va_args will be of no help to you because all it does is point to the beginning byte of the stack where a variable of unknown size starts. The program still has to know the data type.
This is how I impleneted a general-purpose linked list
To insert a node at the head of the linked list.
This is how I impleneted a general-purpose linked list
C Syntax (Toggle Plain Text)
typesed struct node { void * data; // data for this node struct node* next; struct node* prev; } LINK;
To insert a node at the head of the linked list.
C Syntax (Toggle Plain Text)
struct node* insertHead(LINK** head, void* data) { LINK* newNode = malloc(sizeof(LINK)); newNode->data = data; newNode->next = NULL; newNode->prev = NULL; if( *head == NULL) *head = newNode; else { newNode->next = *head; *head = newNode; } }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
I think the node structure should be like this:
where: type is used for type. For example type=1 indicates int, type=2 indicates float, type=3 indicates userdefine and so on...
and Insert function will look like this:
C Syntax (Toggle Plain Text)
struct node { void *data; int type; struct node *next; struct node *prev; }LINK;
where: type is used for type. For example type=1 indicates int, type=2 indicates float, type=3 indicates userdefine and so on...
and Insert function will look like this:
C Syntax (Toggle Plain Text)
int inser(LINK **head, void data, int type) { if(type==1) { // int LINK *newnode = (LINK*)malloc(sizeof(LINK)); newnode->data = malloc(sizeof(int)); } and so on...... }
![]() |
Other Threads in the C Forum
- Previous Thread: Reading a string buffer at the same "output line" as a previous text label
- Next Thread: Pass a string by reference
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures suggestions system systemcall test testautomation unix user voidmain() wab win32api windows.h






