1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Pointer to function

Discussion in 'C' started by theimmortalbg, Jan 23, 2011.

  1. theimmortalbg

    theimmortalbg New Member

    Joined:
    Jan 23, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    hello this is my program

    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    bool isOdd(const int* item)
    {
    return (*item)%2;
    }
    
    int deleteItems (int *array, int itemSize, int itemsCount, bool (*shouldDeleteItem)(const int*))
    {
    for(int i=0; i<itemsCount; i++)
    cout<< shouldDeleteItem (array+i)<< endl;	
    
    return itemsCount*itemSize; 
    }
    
    void main()
    {
    int *p = NULL;
    int n;
    cout<< "Enter length of array: ";
    cin>> n;
    p = new int [n];
    
    cout<< "Numbers in the array: ";
    for(int i=0; i<n; i++)
    cin>> *(p+i);
    
    for(int i=0; i<n; i++)
    {
    deleteItems(p, sizeof(p+i), n, isOdd(p+i));
    }	
    }
    I am trying to call the function deleteItems(int *array, int itemSize, int itemsCount, bool (*shouldDeleteItem)(const int*)) but i can't. Compiler says cannot convert parameter 4 from bool to bool(_cdecl*)(const int *). How i can call delleteItems with last parameter isOdd(p+i). Thanks
     
    Last edited by a moderator: Jan 23, 2011
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    You should just be passing the function pointer like:
    Code:
    deleteItems(p, sizeof(p+i), n, isOdd)


    Jim
     

Share This Page