Advertisement
Guest User

Untitled

a guest
May 25th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int y = 4;
  4.  
  5. // A couple of functions which return pointers-to-ints.
  6. // (Use ints in global scope).
  7. int* f1() {
  8. static int x = 3;
  9. return &x;
  10. }
  11.  
  12. int* f2() {
  13. return &y;
  14. }
  15.  
  16. // Typedef, so the program can be easier to read.
  17. typedef int* (*fPtrInt)();
  18.  
  19. int main(int argc, char **argv) {
  20. fPtrInt a = &f1;
  21. fPtrInt b = &f1;
  22.  
  23. int *p = a();
  24.  
  25. // Check that we can modify static value of f1.
  26. printf("%d\n", *p);
  27. *p = 9;
  28. printf("%d\n", *(f1()));
  29.  
  30.  
  31. // Example of declaring a compound literal which returns
  32. // pointer-to function-pointers-of functions taking no arguments
  33. // returning pointer-to-int.
  34.  
  35. fPtrInt *fpArr = (fPtrInt[]) { a, b };
  36.  
  37. printf("%d\n", *(fpArr[0]()));
  38.  
  39. // And now without the typedef
  40.  
  41. int* ((**fpArr2)()) = (int* (*[])()) { &f1, &f2 };
  42. printf("%d\n", *(fpArr2[0]()));
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement