•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 401,460 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,096 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 343 | Replies: 2
![]() |
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..
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,691
Reputation:
Rep Power: 36
Solved Threads: 877
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
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.
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;
}
} I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Aug 2006
Location: Noida, India
Posts: 157
Reputation:
Rep Power: 3
Solved Threads: 16
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:
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:
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......
}
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
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



Linear Mode