Thread: Pointer passing fails

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    3

    Pointer passing fails

    Hi
    I'm sorry if this is a simple or rather unintelligent question, I am a complete newbie and trying to learn pointers in C. Please bear with me.

    I am trying to write two functions, one calls the other and passes in an argument:

    Code:
    int RegisterImage( const char * streamID, IMGStream & c ) {
    ...
    }
    
    int OpenImage( const char * streamid, int dsize ) {
    ImageStream * im;
    int idx = -1;
    
    if ( GetImgRegIdx(streamid, dsize, im) )
              idx = RegisterImage (streamid, im);
    return idx;
    }
    The calling of "RegisterImage()" fails. It complains about:
    "in passing argument 2 of ‘int Core::RegisterImage(const char*, IMGStream&)’"

    Can anybody help me understand why it's complaining, and how to fix it?
    Thanks so much
    Last edited by Cnewbit; 12-09-2013 at 06:21 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A reference and a pointer are different things. You're trying to supply a pointer to a function expecting a reference.

    im needs initialised so it points at a valid object - your code is not doing that. If you don't also fix that, the fix will get the compiler to accept the code but result in undefined behaviour.

    All you need to do is dereference a pointer (prefix it with an asterix) to get a reference.
    Last edited by grumpy; 12-09-2013 at 06:21 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    3
    Quote Originally Posted by grumpy View Post
    A reference and a pointer are different things. You're trying to supply a pointer to a function expecting a reference.

    I assume that, somewhere, im is initialised so it points at a valid object. If not, the fix will result in undefined behaviour.

    All you need to do is dereference a pointer (prefix it with an asterix) to get a reference.
    Great, that's what I thought at first and I tried this:
    idx = RegisterImage (streamid, &im)

    but it failed with pretty much the same error..i'm so confused! please help

    Thanks

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    An asterix is not an ampersand.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by grumpy View Post
    An asterix is not an ampersand.
    Neither is a comic book character a punctuation character.

    (Asterix == comic book character; asterisk == punctuation character.)

    Pointer passing fails-asterix1-png
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Oh, the gall!

    Pointer passing fails-obelix1-png
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Dec 2013
    Posts
    3
    Thanks guys, but does anybody have a solution to this? can anyone suggest how i can fix this?

    Thanks

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Cnewbit View Post
    Thanks guys, but does anybody have a solution to this? can anyone suggest how i can fix this?

    Thanks
    Asterisk = * ampersand = & they are not interchangeable so don't interchange them thanks

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    If you're using references and compiling C++, it would be better to post this question in the C++ forum. References are not available in C.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Indeed. Moved to C++ programming forum.
    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

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Your RegisterImage function makes use of a IMGStream parameter, the value im that you are attempting to pass to it within the OpenImage function however is of type ImageStream. Is this a typo? Is one a typedef for the other or are they truly different things?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-06-2011, 03:46 AM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. passing method pointer as a function pointer
    By sevcsik in forum C++ Programming
    Replies: 5
    Last Post: 12-30-2007, 06:19 AM
  4. Replies: 4
    Last Post: 11-05-2006, 02:57 PM
  5. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM