User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Apr 2007
Posts: 77
Reputation: phalaris_trip is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
phalaris_trip's Avatar
phalaris_trip phalaris_trip is offline Offline
Junior Poster in Training

Passing type as a parameter; generalised linked list in C?

  #1  
May 14th, 2008
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..
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,691
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 877
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Passing type as a parameter; generalised linked list in C?

  #2  
May 14th, 2008
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
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
Reply With Quote  
Join Date: Aug 2006
Location: Noida, India
Posts: 157
Reputation: Luckychap is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 16
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Junior Poster

Re: Passing type as a parameter; generalised linked list in C?

  #3  
May 15th, 2008
I think the node structure should be 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......



}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 1:31 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC