pointers can be sooo confusing!!! ACK, please help me with this code...

This is a discussion on pointers can be sooo confusing!!! ACK, please help me with this code... within the C++ Programming forums, part of the General Programming Boards category; Alright, I have three BITMAP* s called page1, page2, and page3. They are used for "flipping" pages in my program. ...

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    pointers can be sooo confusing!!! ACK, please help me with this code...

    Alright, I have three BITMAP* s called page1, page2, and page3. They are used for "flipping" pages in my program. Then, I have BITMAP** active_page which POINTS to which page I want to use. Then, when I blit things to active_page it goes REALLY to either page1, page2, or page3. The difficulty comes in when I have to pass these to functions, and compare them. Well, here is my very sad attempt at it. I *know* it can be done! I just need some help! Thanks.
    Code:
    BITMAP *ship, *page1, *page2, *page3, **active_page, *title;
    
    //this is how i call the function
    flip_page(&wPage, &*page1, &*page2, &*page3, &**active_page);
    
    //this is the actual function
    void flip_page(int *wPage, BITMAP** page1, BITMAP** page2, BITMAP** page3, BITMAP*** active_page)
    {
    	if(***active_page == &(*page1))
    	{
            **active_page = &(*page2);
    		*wPage       = 2;
    	}
        if(***active_page == &(*page2))
    	{
            **active_page = *page3;
    		*wPage       = 3;
    	}
    	if(***active_page == &(*page3))
    	{
    		**active_page = *page1;
    		*wPage       = 1;
    	}
    
    }
    Compiling...
    main.cpp
    C:\Documents and Settings\Administrator\Desktop\test allegro\main.cpp(194) : error C2664: 'flip_page' : cannot convert parameter 5 from 'struct BITMAP *' to 'struct BITMAP *** '
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\Documents and Settings\Administrator\Desktop\test allegro\main.cpp(242) : error C2678: binary '==' : no operator defined which takes a left-hand operand of type 'struct BITMAP' (or there is no acceptable conversion)
    C:\Documents and Settings\Administrator\Desktop\test allegro\main.cpp(244) : error C2440: '=' : cannot convert from 'struct BITMAP ** ' to 'struct BITMAP *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\Documents and Settings\Administrator\Desktop\test allegro\main.cpp(247) : error C2678: binary '==' : no operator defined which takes a left-hand operand of type 'struct BITMAP' (or there is no acceptable conversion)
    C:\Documents and Settings\Administrator\Desktop\test allegro\main.cpp(252) : error C2678: binary '==' : no operator defined which takes a left-hand operand of type 'struct BITMAP' (or there is no acceptable conversion)
    Error executing cl.exe.
    Creating browse info file...

    test allegro.exe - 5 error(s), 0 warning(s)

  2. #2
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    and here is an example of when i call some other *working* functions that use active_page:

    draw_aliens(current_alien, &*active_page, aliens);

  3. #3
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    well i got it down to 1 error. here it is:
    Code:
    		flip_page(&wPage, &*page1, &*page2, &*page3, &(**active_page)); //error here
    
    
    void flip_page(int *wPage, BITMAP** page1, BITMAP** page2, BITMAP** page3, BITMAP*** active_page)
    {
    	if(&**active_page == &(*page1))
    	{
            **active_page = &**page2;
    		*wPage       = 2;
    	}
        if(&**active_page == &(*page2))
    	{
            **active_page = &**page3;
    		*wPage       = 3;
    	}
    	if(&**active_page == &(*page3))
    	{
    		**active_page = &**page1;
    		*wPage       = 1;
    	}
    
    }
    Compiling...
    main.cpp
    C:\Documents and Settings\Administrator\Desktop\test allegro\main.cpp(194) : error C2664: 'flip_page' : cannot convert parameter 5 from 'struct BITMAP *' to 'struct BITMAP *** '
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    Error executing cl.exe.
    Creating browse info file...

    test allegro.exe - 1 error(s), 0 warning(s)

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Can we see your whole code, including the structures?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >//this is how i call the function
    >flip_page(&wPage, &*page1, &*page2, &*page3, >&**active_page);

    flip_page(&wPage, &page1, &page2, &page3, &active_page);

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I wonder if this shouldn't be:
    Code:
    //this is the actual function
    void flip_page(int *wPage, BITMAP** page1, BITMAP** page2, BITMAP** page3, BITMAP*** active_page)
    {
    	if(*active_page == page1)
    	{
            *active_page = page2;
    		*wPage       = 2;
    	}
        if(*active_page == page2)
    	{
            *active_page = page3;
    		*wPage       = 3;
    	}
    	if(*active_page == page3)
    	{
    		*active_page = page1;
    		*wPage       = 1;
    	}
    
    }
    This code is a bit too complex for me, but it seems simpler.

  7. #7
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Was this example from a book??

    Why do you have so many double pointers - if there's a reason please fill us in, otherwise single pointers would due just fine.

    Why are you using '&' address of operater on your pointers??
    What's up with the triple BITMAP pointer for active_page??
    Code:
    void flip_page(int *wPage, BITMAP** page1, BITMAP** page2, BITMAP** page3, BITMAP** active_page) //Cut active page down to ptr->ptr
    {
    	if(**active_page == **page1)//Dereferencing
    	{
            active_page = page2;
    		*wPage       = 2;
    	}
        if(**active_page ==** page2)//Dereferencing
    	{
            active_page = page3;
    		*wPage       = 3;
    	}
    	if(**active_page ==** page3)//Dereferencing
    	{
    		active_page = page1;
    		*wPage       = 1;
    	}
    
    }
    
    
    //Call function like this.
    flip_page(&wPage, page1, page2, page3, active_page); //If wPage is already a ptr, drop the '&'
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,702
    Swoopy's code is correct. In order to attach to the pointer, you MUST pass it's address, no matter how deep the pointers go.
    Still, there is one major flaw in the code. Look:


    Code:
    void flip_page(int *wPage, BITMAP** page1, BITMAP** page2, BITMAP** page3, BITMAP*** active_page)
    {
    	if(*active_page == page1) {
                     printf("Active page, 1->2");
                     *active_page = page2;
    	 *wPage       = 2;
    	}
                    if(*active_page == page2) {
                     printf("Active page, 2->3");
                     *active_page = page3;
                     *wPage       = 3;
    	}
    	if(*active_page == page3)  {
    	 printf("Active page, 3->1");
                     *active_page = page1;
    	 *wPage       = 1;
    	}  
     printf("Exiting function...");
    }
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

  9. #9
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    let me clear it up a little. this is an Allegro program for a graphical 2D shooter game. all pictures need to be type BITMAP*, which always has the asterisk (pointer). so, if i want to point to one of those bitmaps (page 1, 2, and 3), i need a "pointer to a pointer". then if i want to pass it to a function, it becomes a "pointer to a pointer to a pointer" if you know what i mean. maybe im wrong, hope that helps though.

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I'm sorry, I have to comment...
    That is the most obfuscated bit of code i've ever seen, and i'm serious. (Besides the programming contests to obfuscate code..)
    Thank you and good night.

  11. #11
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    here is my original code, and the problem was that active_page never changed from page1, it stayed the same. here is the whole main.cpp.
    Attached Files Attached Files

  12. #12
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Code:
    flip_page(&wPage, &page1, &page2, &page3, &active_page);
    
    I think you should at least change this... to
    
    flip_page(wPage, &page1, &page2, &page3, &active_page);
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,702
    We have already shown you how to fix this. Look at all of the posts. Swoopy gave you the function, Rocco gave you the correct passing syntax, and I pointed to a logic error that, until fixed, will not flip the pages.
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

  14. #14
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    1. the code you guys gave does not work (rocco's)
    2. what is the error you pointed out (logic one)

    Compiling...
    main.cpp
    C:\Documents and Settings\Administrator\Desktop\GAME BACKUP #2\test allegro\main.cpp(244) : error C2678: binary '==' : no operator defined which takes a left-hand operand of type 'struct BITMAP' (or there is no acceptable conversion)
    C:\Documents and Settings\Administrator\Desktop\GAME BACKUP #2\test allegro\main.cpp(249) : error C2678: binary '==' : no operator defined which takes a left-hand operand of type 'struct BITMAP' (or there is no acceptable conversion)
    C:\Documents and Settings\Administrator\Desktop\GAME BACKUP #2\test allegro\main.cpp(254) : error C2678: binary '==' : no operator defined which takes a left-hand operand of type 'struct BITMAP' (or there is no acceptable conversion)

    ...comes from rocco's function for the if-statements in teh function:
    Code:
    void flip_page(int *wPage, BITMAP** page1, BITMAP** page2, BITMAP** page3, BITMAP** active_page) //Cut active page down to ptr->ptr
    {
    	if(**active_page == **page1)//Dereferencing
    	{
            active_page = page2;
    		*wPage       = 2;
    	}
        if(**active_page ==** page2)//Dereferencing
    	{
            active_page = page3;
    		*wPage       = 3;
    	}
    	if(**active_page ==** page3)//Dereferencing
    	{
    		active_page = page1;
    		*wPage       = 1;
    	}
    
    }
    END_OF_FUNCTION(flip_page);
    Last edited by Leeman_s; 11-01-2002 at 04:10 PM.

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,702
    Ok. One more time. Here is the code that Swoopy came up with. I added some printing routines so that you can spot the logical error.


    Code:
    void flip_page(int *wPage, BITMAP** page1, BITMAP** page2, BITMAP** page3, BITMAP*** active_page)
    {
       if(*active_page == page1) {
       printf("Active page, 1->2");
       *active_page = page2;
       *wPage       = 2;
       }
       if(*active_page == page2) {
       printf("Active page, 2->3");
       *active_page = page3;
       *wPage       = 3;
       }
       if(*active_page == page3)  {
        printf("Active page, 3->1");
        *active_page = page1;
        *wPage       = 1;
        }  
     printf("Exiting function...");
    }


    Just call the function like this:

    flip_page(wPage, &page1, &page2, &page3, &active_page);

    The first four are *, and the last is a **.
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

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

Similar Threads

  1. Pointers confusing me (Newbie questions)
    By klawson88 in forum C Programming
    Replies: 12
    Last Post: 04-30-2009, 01:14 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Trying to make this code faster & Cramer
    By just2peachy in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2004, 09:54 AM
  5. Convert code to use Pointers, Functions, etc.
    By JYoung in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 11-13-2001, 12:33 PM

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