What is wrong with this? (Passing (pointers to functions) to functions)

This is a discussion on What is wrong with this? (Passing (pointers to functions) to functions) within the C++ Programming forums, part of the General Programming Boards category; #include "iostream.h" #include "conio.h" void ShowBoard(int Board[][6]); void BoardSetup(int Board[][6]); void MoveBlockDown(int Board[][6], int (*ppShowBoard)(int [][6])); int main() { int ...

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

    What is wrong with this? (Passing (pointers to functions) to functions)

    #include "iostream.h"
    #include "conio.h"

    void ShowBoard(int Board[][6]);
    void BoardSetup(int Board[][6]);
    void MoveBlockDown(int Board[][6], int (*ppShowBoard)(int [][6]));

    int main()
    {
    int GameBoard[9][6];
    int (*pfunc)(int [][6])=ShowBoard;
    BoardSetup(GameBoard);
    ShowBoard(GameBoard);
    MoveBlockDown(GameBoard, pfunc);

    return 0;
    }
    //************************************************** *****
    void ShowBoard(int Board[][6])
    {
    for(int i=1;i<=8;i++)
    {
    for(int p=1;p<=6;p++)
    {
    cout<<Board[i][p];
    }
    cout<<endl;
    }
    }
    //************************************************** *****
    void BoardSetup(int Board[][6])
    {
    for(int i=1;i<=8;i++)
    {
    for(int p=1;p<=6;p++)
    {
    Board[i][p]=0;
    }
    cout<<endl;
    }
    for(i=1;i<=6;i++)
    Board[9][i]=2;
    }
    //************************************************** *****
    void MoveBlockDown(int Board[][6], int (*ppShowBoard)(int [][6]))
    {
    int temp;
    temp=Board[1][3];
    do
    {
    int i=0;
    i++;
    if(Board[i][1]==2)
    break;
    Board[i][3]=9;
    Board[i-1][3]=temp;
    ppShowBoard(Board);
    }
    ppShowBoard(Board);
    }



    Ok, the main thing I want to do is be able to call ShowBoard in MoveBlockDown. Is this the way? If so, why doesnt it work?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Is this the way? If so, why doesnt it work?
    There is no need to use a function pointer. You can just call showboard normally. why doesn't it work? MISMATCHED TYPES. look at your function pointer return value and the function you are assigning to the pointer return value. one is int the other void.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing pointers outside of functions
    By thomas41546 in forum C Programming
    Replies: 6
    Last Post: 01-26-2008, 06:00 PM
  2. Pointers to Class Member Functions
    By Dark_Phoenix in forum C++ Programming
    Replies: 6
    Last Post: 09-02-2007, 03:21 PM
  3. Replies: 7
    Last Post: 04-19-2006, 12:17 PM
  4. Pointers and reference passing
    By Denis Itchy in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2002, 01:36 AM
  5. Passing variables by pointers...what am I doing wrong?
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 06-07-2002, 03:10 PM

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