pointers in functions

This is a discussion on pointers in functions within the C Programming forums, part of the General Programming Boards category; Hi =] When Should I use pointer in functions? example: int something(int *i) int something(int i) Thanks...

  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,220
    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,475
    Psss by value == no pointer
    pass by reference = use pointers as C does not have the reference oprerator '&'
    I'm just trying to be a better person - My Name Is Earl

  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,211
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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, 12: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, 06:15 AM
  4. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21