Wednesday, 31 August 2011

Callback functon - Fixed

I just love people who make the effort and help to teach others new stuff. But with code samples i don't understand why you don't test the code before publishing. I found an interesting example off callback function example but it was not working. Here is the proper stuff:)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct
  4. {
  5.  int age;
  6.  char name[10];
  7.  char surname[10];        
  8. }person;

  9. // Simple fuction to be called as callback
  10. void callMe(person *m)
  11. {
  12.  printf("%d %s %s \n\r", m->age, m->name, m->surname);
  13. }

  14. int main(int argc, char *argv[])
  15. {
  16.   // Declaration of pointer to function with same signiture as callMe
  17.   void (*callback)(person*)
  18. ;
  19.   // Create struct and fill with data
  20.   person s;
  21.   s.age = 27;
  22.   //Don't forget how to copy strings in C/C++
  23.   strcpy(s.name, "Dejan");
  24.   strcpy(s.surname, "Cencelj");
  25.  
  26.   // Init
  27.   callback = (void*)callMe;
  28.   // Call to callMe with callback
  29.   callback(&s);
  30.  
  31.   system("PAUSE"); 
  32.   return 0;
  33. }
Original


No comments:

Post a Comment