Thread: When best to use pointers in C

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    73

    When best to use pointers in C

    Hi again everybody. after i solved my rather stupid syntax problem (forgot to add & in some function). I am new to C (coming from java) and i want to know when i should pass pointers to functions and in what cases i should pass a "Raw" integer directly into function.
    Right now it seens that i maybe over use pointers

    I know that when i pass an int, this is actually makes a copy of that int and pass it into the function. so why not use pointers every where i can?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    When you pass an int, a copy of it's value is passed to the function. When you pass a pointer, a copy of the variable's address is passed to the function. So both cases pass something and there's nothing to gain from passing a pointer.


    You pass a pointer for three reasons.
    1. You need to modify the original value in the calling function.
    2. You're passing a struct and use a pointer so less bytes need to be passed to the function.
    3. You're passing an array, which is passed as a pointer to the first element instead of making a copy of the entire array.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Generally you will pass a pointer (to an object) into a function if you want to modify the object being pointed to.

    The other common use is when you're passing something you don't want to modify but passing the object as value (which creates a [shallow] copy of the object) but the value is an aggregate type (e.g. struct) which would cause unnecessary copying of memory.

    For example, given:

    Code:
    struct foo {
      int a;
      int b;
      // ...
      // ...
      int z;  // pretend all 26 integers named a through z are in the struct
    };
    and a function

    Code:
    void function(struct foo bar);
    Then calling "function" will make a copy of 'bar'; i.e. it will copy [at least] 26 integers (to the new, 'local copy' of bar) when you call the function.

    If the function was:

    Code:
    void function(const struct foo *bar);
    Then only the address is passed.

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    73
    Thank you for the explanations. now i understand that there is no gain in passing a pointer instead of an int, when i don't actually need to modify anything. But if i still choose to do that, are there downsides for that? I mean..other than that it is not neccesary, is there any other reason why not do do so?

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The main reason not to do it is that it communicates the wrong thing to other programmers. If I see you passing a single int as a pointer, I assume you're going to modify it. If you don't then I'm wondering what you're up to.


    It's also potentially slower to access the value through a pointer then to access the value directly.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    73
    ok thanks! I will now use pointers only when needed

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by patishi View Post
    Thank you for the explanations. now i understand that there is no gain in passing a pointer instead of an int, when i don't actually need to modify anything. But if i still choose to do that, are there downsides for that? I mean..other than that it is not neccesary, is there any other reason why not do do so?
    It would mean you couldn't call the function with an integer literal (e.g. funct(&42) isn't valid, but funct(42) is).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 43
    Last Post: 05-23-2013, 03:01 PM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  4. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM

Tags for this Thread