title: A Generic Compare Function , With A Variable Condition
body: <p>I'm trying to use the function </p>
  
  <pre><code>List listFilter(List list , CompareFunction func);
  </code></pre>
  
  <p>which , as you all well know , returns a filtered list according to a specific condition , from a given list; the param list .</p>
  
  <p>Here's the definition of CompareFunc :</p>
  
  <pre><code>typedef int (*CompareFunc)(ListElement);
  </code></pre>
  
  <p>ListElement is of course void* .</p>
  
  <p>I'm currently working on a problem in which a ListElement is an object that stores information regarding a person , whereas the information includes their age .</p>
  
  <p>I'm trying to write a function which groups a given list of people based on their age, which can obviously vary, therefor I'm trying to write a function which compares a ListElement against an int , i.e a person's age against an age category , respectively , suppose it's defined thusly :</p>
  
  <pre><code>bool ageIsValid(ListElement person , int ageCategory);
  </code></pre>
  
  <p>and pass it down as a parameter to listFilter .
  However, the problem that I've encountered is that CompareFunc's definition is not decided by me , i.e , I'm the user of the ADT List , so I can't pass ageIsValid directly to listFilter.</p>
  
  <p>Is there an available solution which doesn't rely on global variables ? , since it's frowned upon in this specific course that I'm currently taking .</p>
  
  <p>All answers would be greatly appreciated :)</p>
  
  <p>EDIT : </p>
  
  <p>I just had an idea , and I was wondering if it'd work :</p>
  
  <p>I'll define a static function like so :</p>
  
  <pre><code>    static List listFilterCustom(List list , int age){
          static bool filterByAge(ListElement element){
              /* pAge = getAgeOfPerson(element); */
              return pAge == age;
          }
          return listFilter(list , filterByAge);
       }
  </code></pre>
  
  <p>i.e , I'll define a function inside the scope of listFilterCustom , in which the variable age is recognized , thus excluding the use of global variables .
  Is this solution valid ?</p>