Thread: passing (...) as parameter

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95

    passing (...) as parameter

    Code:
    LE** listConstructor(int n2,...){
    	int i=0;
    	.... do something with the input
    }
    What I want to do, is that for as many int parameters passed something is done with the integers passed. How do I do that, without determining the size of the parameters in advance, but using ... ?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    look for va_arg sample
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    Hello,

    I've looked into the function passed, absolutely interesting. However I realized I don't know how I could you use it in my intended purpose, i.e declaration and definition of a linked list of integers.

    The 'dirty' way is:

    Code:
    struct le{
    	int key;
    	struct le* next;
    	struct le* prev;
    } ; typedef struct le LE;
    
    in main: 
    LE* list = (LE*) malloc(sizeof(LE));
    	list->key = 2;
    	list->next = (LE*) malloc(sizeof(LE));
    	list->next->key = 14;
    	list->next->next = (LE*) malloc(sizeof(LE));
    	list->next->next->key = 56;
    	list->next->next->next = (LE*) malloc(sizeof(LE));
    	list->next->next->next->key = 98;
    	list->next->next->next->next = (LE*) malloc(sizeof(LE));
    	list->next->next->next->next->key = 4;
    	list->next->next->next->next->next = (LE*) malloc(sizeof(LE));
    	list->next->next->next->next->next->key = 34;
    	list->next->next->next->next->next->next = (LE*) malloc(sizeof(LE));
    	list->next->next->next->next->next->next->key = 18;
    	list->next->next->next->next->next->next->next = (LE*) malloc(sizeof(LE));
    	list->next->next->next->next->next->next->next->key = 1;
    	list->next->next->next->next->next->next->next->next = (LE*) malloc(sizeof(LE));
    	list->next->next->next->next->next->next->next->next->key = 0;
    	list->next->next->next->next->next->next->next->next->next = (LE*) malloc(sizeof(LE));
    	list->next->next->next->next->next->next->next->next->next->key = 88;
    But any loop/recursive solution? Besides above I didn't allocates and point the prev pointers, which would double the lines of code.
    Last edited by simpatico_qa; 03-26-2009 at 04:07 PM.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    start by providing some API for working with your list

    like

    list_inseart(LE** pList, int newValue);
    list_print(LE* List);

    etc

    when you'll have such functions for working with the list - writing a function that adds arbitrary number of integers will be a lot easier
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    But if so, initializing the listnode shall always be the long way I did? I've functions that work on the list but they don't declare and define nodes.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by simpatico_qa View Post
    But if so, initializing the listnode shall always be the long way I did? I've functions that work on the list but they don't declare and define nodes.
    why?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    because I asked for code that given a size and/or values will declare and define a list node accordingly. Yet u replied with something else (creating elements one at the time, when needed).

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by simpatico_qa View Post
    because I asked for code that given a size and/or values will declare and define a list node accordingly. Yet u replied with something else (creating elements one at the time, when needed).
    vart is just suggesting good code architecture. It doesn't bear directly on variable arguments but it's still good advice.

    I would suggest a function skeleton like this:

    Code:
    #include <stdarg.h>
    
    LE* listConstructor(int n2, ...)
    {
        LE *list = NULL;
        va_list valist;
    
        va_start(valist, n2);
        while(n2 > 0)
        {
            list = listAppend(list, va_arg(valist, LE *));
            --n2;
        }
        va_end(valist);
        return list;
    }
    This assumes a listAppend() function which appends the given element to the list... In practice this function will be a bit more complicated, but it shows how to use the va_* functions
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    Okay..I've several problems:

    1. I'm trying to set the 'backward/previous' links for a doubly linked list.
    My method is the following:
    Code:
    void setPrevLinks(LE* lis){
    	if (lis != NULL) return;
    	else{
    		lis->next->prev = lis;
    		setPrevLinks(lis->next);
    		return;
    	}
    }
    and I test it with:

    Code:
    setPrevLinks(list);
    	printf("%d\t",list->next->prev->key);
    Yet I get a bus error.

    Any feedback?

    [/code]

  10. #10
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    Hallo, any help on this?

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by simpatico_qa View Post
    Code:
    void setPrevLinks(LE* lis){
    	if (lis != NULL) return;
    So, you return if the pointer is NOT null? And you continue if it is? Hmm...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  5. typecasting data for parameter passing
    By daluu in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 02:58 PM