Using a function pointer as a function argument

This is a discussion on Using a function pointer as a function argument within the C++ Programming forums, part of the General Programming Boards category; Hello all, I am trying to learn C++ on my spare time and am following the book "c++ primer plus" ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Using a function pointer as a function argument

    Hello all,

    I am trying to learn C++ on my spare time and am following the book "c++ primer plus" right now.. In one of the exercises I want to be able to use a function as one of the arguments to an other function. However, the function inside of the other functions arguments is not being called.. here is a piece of the code...

    The prototype of the two functions:
    Code:
    int average_score(int ar[], int limit);
    void display_scores(const int ar[], int limit, int (*avg_score)(int*, int));
    The function call:

    Code:
    display_scores(golf_scores, num_of_scores, average_score);
    and the definitions:
    Code:
    int average_score (int ar[], int limit)
    {
    	int average = 0;
    	int x;
    	for (x = 0; x < limit; x++)
    	{
    		average = average + ar[x];
    	}
    	average = average / limit;
    	cout << "This is function average_score() " << endl ;
    	return average;
    }	
    
    void display_scores(const int ar[], int limit, int (*avg_score)(int*, int))
    {
    	int x;
    	for (x = 0; x < limit; x++)
    	{
    		cout << ar[x] << " ";
    	}
    	cout << endl << "The average score is " << avg_score << endl;
    }
    I know that it is not necessary to do it like this, but I would like to know why it doesn't work. I will attach the entire piece of code.. Thanks to anyone who can help me.. I worked on this problem for several hours tonight and could not figure it out.
    Attached Files Attached Files

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    That's because you're not calling the function:
    Code:
    cout << endl << "The average score is " << avg_score << endl;
    You don't call a function with the name alone. You need to supply the arguments (or an empty set of parenthesis if there are none)
    Try something like this:
    Code:
    cout << endl << "The average score is " << avg_score(ar, limit) << endl;
    Function pointers are cool in C, but in C++, I would recommend using function objects which overload operator().
    Last edited by NeonBlack; 10-09-2010 at 09:59 PM.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    If you wanted that to be clearer:

    Code:
    typedef int (*func)(int*, int);
    
    void display_scores(const int ar[], int limit, func avg_score){
    
    }
    third parameter is typedef'd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. C++ Function Pointer Problems
    By CodeBugs in forum C++ Programming
    Replies: 2
    Last Post: 07-28-2009, 08:06 PM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM

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