Thread: Trouble with double pointers

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    Question Trouble with double pointers

    In our code we are using double pointers, and when compiling we are receiving error messages from all the double pointers that are called through another function acting as an interface, but no problem arises from the pointers straight between the main function and the function in question. What is causing the double pointers to be incorrect when they are called from a function and not from the main? I hope this is understandable.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Show the smallest and simplest program that demonstrates the error, and also post the error message.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2
    OK, we have a program that uses a doubly linked list. *top points the the first element, *bottom points to the last.
    Certain functions, like the swap function use double pointers as arguments to change where top and bottom point to.
    Code:
    void swap(song_t *a, song_t *b, song_t **top, song_t **bottom)
    ...
    *top=b;
    when calling these functions straight from the main function, we have no problem:
    Code:
    swap(a, b, &top, &bottom)
    the problem is we also have interfaces, functions that call other functions. when the interface calls a function that has these double pointers as arguments, it gives us the error message like "passing arg 3 of 'swap' from incompatible pointer type"
    right now the interface has **top and **bottom as arguments and tries to pass &top and &bottom as arguments to other functions.
    Is there some other way we should be doing this?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cat1
    the problem is we also have interfaces, functions that call other functions. when the interface calls a function that has these double pointers as arguments, it gives us the error message like "passing arg 3 of 'swap' from incompatible pointer type"
    right now the interface has **top and **bottom as arguments and tries to pass &top and &bottom as arguments to other functions.
    Then why pass the addresses of top and bottom when you should be passing top and bottom?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4
    This is why you should probably use different variable names. I'm just guessing at your problem but I'm pretty sure its this.

    top and bottom are both single pointers in main. So you pass the address to make them double pointers. Wham, you understand that I know.

    However, inside of your function swap, top and bottom are already double pointers. Therefore you just pass just top and bottom. No addresses, no dereferences, just top and bottom.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  4. Replies: 12
    Last Post: 10-23-2006, 07:45 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM