SHARE
TWEET

Callback and function pointers

math230 Jun 25th, 2020 861 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<math.h>
  5.  
  6. int x = 3;  //global variable
  7.  
  8. int getNextRandomValue(void);
  9. void populate_array(int *array, size_t arraySize, int (*getNextValue)(void));
  10.  
  11. int main()
  12. {
  13.     int c = 1;
  14.     float a=3.1, b=3.5;
  15.     int myarray[10];
  16. //    populate_array(myarray, 10, getNextRandomValue);  //option
  17.    populate_array(myarray, 10, &getNextRandomValue);
  18.     printf(" myarray[2] is %d \n", myarray[2]);
  19.  
  20.     return 0;
  21. }
  22.  
  23. int getNextRandomValue(void){
  24.     return rand()%100;
  25. }
  26.  
  27. //option
  28. //void populate_array(int *array, size_t arraySize, int (*getNextValue)(void)){
  29. void populate_array(int *array, size_t arraySize, int getNextValue(void)){
  30.     for (size_t i=0; i<arraySize; i++)
  31.         array[i] = getNextValue();
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top