Best Practices for Operations on Structures

This is a discussion on Best Practices for Operations on Structures within the C Programming forums, part of the General Programming Boards category; Just use a pointer, pass a pointer to what you want to alter, and do it that way. (Or a ...

  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,185
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread


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