Thread: Best Practices for Operations on Structures

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just use a pointer, pass a pointer to what you want to alter, and do it that way. (Or a pointer to a pointer, and allocate the data inside the function.)


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

  2. #17
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Quote Originally Posted by quzah View Post
    Just use a pointer, pass a pointer to what you want to alter, and do it that way. (Or a pointer to a pointer, and allocate the data inside the function.)


    Quzah.
    This is for the return value. The options I see are returning a pointer to the new data or passing a destination pointer to the function and assigning the data there (see original post.)

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or pass a pointer to a pointer if you don't feel like returning. It doesn't matter how you do it really. It's just a matter of preference.


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

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Passing in a destination pointer only works if all your objects are the same size (so that the caller knows how big a malloc to do, or whatever). Between returning a pointer to a new data and passing in a double pointer, there's not a lot of difference and I'm pretty sure I've seen both used a fair bit. The second is a little more flexible if there's something else you want to return, like a size or an error code or something; the first allows nesting functions if that's something you might see yourself doing.

  5. #20
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Thanks for the help. Also thanks for pointing out that I need a pointer to a pointer if I allocate the data.

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I said all of that in the first reply to this thread. And in pretty much every other reply to this thread as well...


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

Tags for this Thread