Thread: pointer to pointer ok?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    23

    pointer to pointer ok?

    Hi,

    I remember reading a while back that pointers to pointers was in some way bad programming practice:

    e.g. A string swapping function

    Code:
    void swap(char** a, char ** b)
    {
    char * temp = *a
    *a = *b
    *b = *a
    }

    Could someone tell me why again please?

    Thanks
    Last edited by brif; 09-14-2002 at 09:03 AM.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I dont believe it is bad programming practice at all. In fact in c its necessary if you need to pass a pointer to a function by reference rather than by value.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    It is not bad practice but looks ugly.
    Usually your using a struct to pass data
    and so using pointers to pointer won't happen much.
    For example you would have
    struct Foo
    {
    char* s1;
    char* s2;
    };

    void swap(struct Foo* f);

    For sorting, using a non inline
    function could slow down the sort though.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    16
    what do you mean by non inline function?

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    "inline" is a instruction to the compiler. What it says is instead of jumping to the code of a function when you call it, it copies the code inside that function there instead......

    This can be much quicker as each time you call a function, certain code must be produced to allow your code to pass parameters, set up stack frames and then return when the function is done...with an inline you can get away from this overhead.......It should only be used for small functions though....your compiler might not allow you to use it for large functions

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    IMO, there's nothing wrong with having pointers to pointers.

    It's perfectly legal in C and I use it all the time. The problem is that other people who read such code won't be able to grasp it very easily.

    One example is instead of returning a pointer to a new address, assign the new address in the function. All those indirection operators, '*', would make the code look ugly. But on the other hand, if you're the only one who's gonna read the code, then you can have all the pointers you like.

  7. #7
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by The Dog
    IMO, there's nothing wrong with having pointers to pointers.

    It's perfectly legal in C and I use it all the time. The problem is that other people who read such code won't be able to grasp it very easily.

    One example is instead of returning a pointer to a new address, assign the new address in the function. All those indirection operators, '*', would make the code look ugly. But on the other hand, if you're the only one who's gonna read the code, then you can have all the pointers you like.
    if a programmer cannot understand double-pointers, then it's his problem, not yours. i mean, main() takes a double-pointer (unless you don't want to use commandline params)!! it's a pretty common concept.
    hello, internet!

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Originally posted by moi
    if a programmer cannot understand double-pointers, then it's his problem, not yours. i mean, main() takes a double-pointer (unless you don't want to use commandline params)!! it's a pretty common concept.
    The message I was trying to convey, was that if you don't really need to use pointers to pointers, or even pointers for that matter, then don't use them. It will decrease one's chances of getting memory leaks, and the code would be more readable.

    IMO, of course.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Fordy
    "inline" is a instruction to the compiler. What it says is instead of jumping to the code of a function when you call it, it copies the code inside that function there instead......
    It should be noted that C has no 'inline' keyword. This is a C++ feature.

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

  10. #10
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    and an (of course non-portable) extension on some C compilers
    hello, internet!

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by quzah
    It should be noted that C has no 'inline' keyword. This is a C++ feature.

    Quzah.
    Hmm......I thought it was added with C99 (I could well be wrong, but that's the impression I had)...but then I dont know what compilers are out there that comply to this anyway.....
    Last edited by Fordy; 09-15-2002 at 10:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM