C Tutorial

C Function Pointers

A function pointer stores the address of a function. Pass it to qsort or call it later.

The address of a function

A pointer to an int stores the address of an integer. A function pointer stores the address of a function. You can assign it, pass it, and call through it. The type has to match: return type and parameter types.

The declaration looks noisy because the star sits in parentheses:int (*fp)(int, int). That means fp is a pointer to a function that takes twoints and returns an int. Without those parentheses, int *fp(int, int)would be a function that returns int *.

Declare, assign, call

Assign a function name to the pointer. In C the name decays to an address, so fp = add andfp = &add are both fine. Call with fp(3, 4).

Example

#include <stdio.h>

int add(int a, int b) {
  return a + b;
}

int mul(int a, int b) {
  return a * b;
}

int main(void) {
  int (*fp)(int, int);
  fp = add;
  printf("%d\n", fp(3, 4));
  fp = mul;
  printf("%d\n", fp(3, 4));
  return 0;
}

First call prints 7. After you reseat fp to mul, the same call syntax prints 12. The caller does not name add or mul at the call site — it names fp.

Pass a function pointer

A parameter can be a function pointer. The helper calls whatever you pass. That is a callback: the library (or your helper) decides when, you decide what.

Example

#include <stdio.h>

int add(int a, int b) {
  return a + b;
}

int apply(int a, int b, int (*fp)(int, int)) {
  return fp(a, b);
}

int main(void) {
  printf("%d\n", apply(10, 5, add));
  return 0;
}

apply does not know it is adding. It only knows the pointer’s type. Swap in another function with the same signature and apply stays unchanged.

qsort needs a comparator

qsort in <stdlib.h> sorts a block of memory. It does not know your element type. You pass a comparator: a function that takes two const void * pointers and returns a negative number, zero, or a positive number — less than, equal, or greater.

Example

#include <stdio.h>
#include <stdlib.h>

int cmp_int(const void *a, const void *b) {
  int x = *(const int *)a;
  int y = *(const int *)b;
  return (x > y) - (x < y);
}

int main(void) {
  int nums[] = {9, 2, 7, 1, 4};
  qsort(nums, 5, sizeof(int), cmp_int);
  for (int i = 0; i < 5; i++) {
    printf("%d ", nums[i]);
  }
  printf("\n");
  return 0;
}

The third argument is the size of one element. The fourth is the function pointer. qsort callscmp_int as it sorts. Output is 1 2 4 7 9. Cast each void * toconst int * before you read the integers.

Keep the type exact

The pointer type must match the function. int (*fp)(int, int) will not accept a function that takes one int, or one that returns void. For qsort, the comparator signature is fixed by the library. Copy it, then fill in the body.

Compile these at /c/try. The editor has a qsort sample too. That URL is gcc for C, not

Next: FILE * so a program can write a file and read it back.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.

Physics

KE or PE, chosen at run time

Gravitational PE near Earth is m g h. KE is ½ m v². Both take two doubles if you freeze g at 9.81 and pass (m, v) or (m, h). A function pointer stores which formula to use.

energy(2, 3) is 9 J of KE or about 58.9 J of PE. The arguments mean different physical quantities. The type system cannot know that. You have to.

KE = ½ m v², PE = m g h

Example

#include <stdio.h>

double ke(double m, double v) {
  return 0.5 * m * v * v;
}

double pe(double m, double h) {
  return m * 9.81 * h;
}

int main(void) {
  double (*energy)(double, double);
  energy = ke;
  printf("KE = %.1f J\n", energy(2.0, 3.0));
  energy = pe;
  printf("PE = %.1f J\n", energy(2.0, 3.0));
  return 0;
}

Maths

Add or multiply

A calculator mode is a function pointer: same two integers, different operation. 6 × 7 is 42. Swap in add and the same call site prints 13.

Example

#include <stdio.h>

int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }

int main(void) {
  int (*op)(int, int) = mul;
  printf("%d\n", op(6, 7));
  return 0;
}

FAQ: C Function Pointers

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c function pointers examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c function pointers in this C C lesson (C Function Pointers).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.