Thread: function with pointers (sort of)

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    43

    function with pointers (sort of)

    If I have a line:

    Code:
    head = insert(head, temp);
    and the insert function to which it refers is:

    Code:
    Pnt *insert(Pnt *list, Pnt *element)
    {
       element->next = list;
       return (element);
    }
    Is there a quick way to amalgamate the code so I just have 1 line and no seperate function?

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    temp->next = head;
    head = temp;
    ?

    Although the function doesn't make a terrible amount of sense. Something to do with a linked list?

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    43
    Yes it is a linked list.

    The little function that I want to amalgamate into the big one is for joining one list with another list. I have it all working as it is now, but I wish to eliminate that function because it is so small. Yet when I tried various things it stopped working.

    Ah yes you are right SMurf, that does work. Thankyou

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's very poor design. You don't simply eliminate functionality because it's "too small". Small functions are good. No one wants to read a 500 line function just because it's "big". The idea is actually to break everything down into very small steps, using functions that do one job and do it correctly. Rather than one massive function that tries to do everything at once. No one wants to read that tihs.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    There's no lower limit for sensible function sizes. Just break the code into logical chunks. Here's one of mine:
    Code:
    void KillTile (tile* this)
    
    // Destroy a tile
    
    {
        free (this);
    }
    It could be argued that this is unnecessary, but makes the code easier to read elsewhere.
    It mirrors the "CreateTile" function and if I need to add any "destructor" stuff, there is a logical place for it.

    If you want to remove the overhead of calling the function for performance reasons, you could use the inline qualifier, if your compiler supports it.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    42
    Also, chris1985, I've found that with optimizations small functions such as this are usually inlined in the final program anyway.

    If you're using gcc then the -S switch should output assembly code. Inspection of this code will tell you if the function call is still being made or if it was inlined.

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. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Replies: 10
    Last Post: 09-15-2004, 01:00 PM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM