Thread: pointers in functions

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    1

    pointers in functions

    Hi =]
    When Should I use pointer in functions?

    example:
    int something(int *i)
    int something(int i)

    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    when you need change something you will have to pass a pointer to it.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> when you need change something you will have to pass a pointer to it.

    Not always, or else there wouldn't be a const attribute.
    Also if you are passing an array to the function.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Psss by value == no pointer
    pass by reference = use pointers as C does not have the reference oprerator '&'
    Double Helix STL

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No but it does have the address operator '&' , as Brian said you should pass a pointer when you wish to change its value, passing by value also uses more resources.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by zacs7 View Post
    passing by value also uses more resources.
    If you don't mind, I'd like to know how you come to this conclusion. Are you referring to the passing of structs or C++ objects?
    Last edited by MacGyver; 05-20-2007 at 04:13 AM. Reason: Correction

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by MacGyver View Post
    If you don't mind, I'd like to know how you come to this conclusion. Are you referring to the passing of structs or C++ objects?
    passing pointer - you are taking sizeof(void*) bytes on stack
    passing struct - you allocating whole struct on stack (and instead of copying just pointer to the temp var - you need to copy all the struct there which can take more time)
    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

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by vart View Post
    passing pointer - you are taking sizeof(void*) bytes on stack
    passing struct - you allocating whole struct on stack (and instead of copying just pointer to the temp var - you need to copy all the struct there which can take more time)
    Structs weren't being discussed prior to this, so that is why I'm asking if that is what he was referring to. The example given by the OP was using integers

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I was talking about the use of passing the address rather the value (mainly stucts). And I read the 'idea' in a book...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  2. HELP WITH FUNCTIONS and POINTERS!!!
    By cjohnson412 in forum C++ Programming
    Replies: 4
    Last Post: 08-11-2008, 10:48 PM
  3. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  4. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM