Skip to content

Instantly share code, notes, and snippets.

@GasparVardanyan
Last active October 11, 2021 14:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GasparVardanyan/da1d451f5d04cdfd4e0f0095c116c122 to your computer and use it in GitHub Desktop.
Save GasparVardanyan/da1d451f5d04cdfd4e0f0095c116c122 to your computer and use it in GitHub Desktop.
Something useful about function pointers in C.
# include <stdio.h>
# include <stddef.h>
// x: A function taking short and returning char.
char x (short); // Usage: char c = x ('H');
// x (); // A function
// x (short); // taking short
// char x (short); // and returning char.
// y: A pointer to a function taking short and returning char.
char (* y) (short); // Usage: char c = (* y) ('E');
// * y; // A pointer
// (* y) (); // to a function
// (* y) (short); // taking short
// char (* y) (short); // and returning char.
// // Array: char (* arr [COUNT]) (short);
// // Array usage: (* arr [index]) ('A');
// z: A function taking two ints and returning pointer
// to a function taking short and returning char.
char (* z (int, int)) (short); // Usage: char c = (* z (1, 2)) ('L');
// z () // A function
// z (int, int) // taking two ints
// * z (int, int) // and returning pointer
// (* z (int, int)) () // to a function
// (* z (int, int)) (short) // taking short
// char (* z (int, int)) (short) // and returning char.
// w: A pointer to a function taking two ints and returning
// pointer to a function taking short and returning char.
char (* (* w) (int, int)) (short); // Usage: char c = (* (* w) (1, 2)) ('L');
// * w; // A pointer
// (* w) (); // to a function
// (* w) (int, int); // taking two ints
// * (* w) (int, int); // and returning pointer
// (* (* w) (int, int)) (); // to a function
// (* (* w) (int, int)) (short); // taking short
// char (* (* w) (int, int)) (short) // and returning char.
// // Array: char (* (* arr [COUNT]) (int, int)) (short);
// // Array usage: (* (* arr [index]) (1, 2)) ('B');
char * (* (* a (long, long, long)) (int, int)) (short);
char * (* (* (* b) (long, long, long)) (int, int)) (short);
char * (* (* (* c (double, double, double, double)) (long, long, long)) (int, int)) (short);
char * (* (* (* (* d) (double, double, double, double)) (long, long, long)) (int, int)) (short);
char * (* (* (* (* e (char, char, char, char, char)) (double, double, double, double)) (long, long, long)) (int, int)) (short);
char * (* (* (* (* (* f) (char, char, char, char, char)) (double, double, double, double)) (long, long, long)) (int, int)) (short);
// Typedefs
typedef char X (short); // Usage: X _x; // For function definitions only.
typedef char (* Y) (short); // Usage: Y _y = & x;
// // Array: Y _yy [COUNT];
typedef char (* Z (int, int)) (short); // Usage: Z _z; // For function definitions only.
typedef char (* (* W) (int, int)) (short); // Usage: W _w = & z;
// // Array: W _ww [COUNT];
char x (short a) { return (char) a; }
char (* z (int a, int b)) (short) { return & x; }
# define NL() putchar ('\n')
int main (void)
{
y = & x;
w = & z;
putchar (x ('H'));
NL ();
putchar ((* y) ('E'));
NL ();
putchar ((* z (1, 2)) ('L'));
NL ();
putchar ((* (* w) (1, 2)) ('L'));
NL ();
puts (":D");
// char (* yy [5]) (short) = {& x, & x, & x, & x, & x};
// char (* (* ww [5]) (int, int)) (short) = {& z, & z, & z, & z, & z};
Y yy [5] = {& x, & x, & x, & x, & x};
W ww [5] = {& z, & z, & z, & z, & z};
for (size_t i = 0; i < 5; i++)
putchar ((* yy [i]) ('A'));
NL ();
for (size_t i = 0; i < 5; i ++)
putchar ((* (* ww [i]) (1, 2)) ('B'));
NL ();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment