LearnFrenzy
Sign in Take a free test

C Programming · Topic 19 of 23

Complicated Declarations

  • 4 exercises
  • 40 questions
  • ~60 min
  • Answers explained

Introduction & Syntax

Complicated Declarations · Study material

Complicated declarations in C – such as int (*p)[5], int (*fp)(int) or void (*signal(int, void (*)(int)))(int) – look cryptic, but they follow a small set of rules. A declaration mirrors how the name is used: int *p says "*p is an int". Once you know that [] and () bind tighter than *, and how to read from the name outwards, any declaration can be decoded. Interviewers love these questions, and real code uses them for callbacks, dispatch tables and 2D arrays. This page teaches the reading method, then gives you 40 practice questions checked on a real compiler.

On this page

  • The right-left rule for reading any declaration
  • Arrays of pointers vs pointers to arrays
  • Function pointers, arrays of them and functions returning them
  • const placement, typedef, C23 typeof and illegal declarations

The right-left rule

Start at the identifier. Look right for [] (array of) or () (function returning); when you hit a ) or the end, look left for * (pointer to), qualifiers and finally the base type. Parentheses group: finish everything inside them first.

DeclarationRead as
int *p[5]p is an array of 5 pointers to int
int (*p)[5]p is a pointer to an array of 5 int
int *f(void)f is a function returning a pointer to int
int (*f)(void)f is a pointer to a function returning int
int (*f[3])(int)f is an array of 3 pointers to functions (int) returning int
int (*(*f)(void))[4]f is a pointer to a function returning a pointer to an array of 4 int
char **argvargv is a pointer to a pointer to char
void (*signal(int, void (*)(int)))(int)signal is a function (int, pointer to function (int) returning void) returning a pointer to function (int) returning void

Arrays of pointers vs pointers to arrays

The parentheses make the difference. An array of pointers is many pointers (40 bytes for 5 on a 64-bit system); a pointer to an array is one pointer whose step size is the whole array.

#include <stdio.h>

int main(void) {
    int row[5] = {1, 2, 3, 4, 5};
    int *ap[5];              /* ap: array of 5 pointers to int   */
    int (*pa)[5] = &row;     /* pa: pointer to an array of 5 int */

    for (int i = 0; i < 5; i++)
        ap[i] = &row[i];
    printf("sizeof ap = %zu, sizeof pa = %zu\n", sizeof ap, sizeof pa);
    printf("*ap[2] = %d, (*pa)[2] = %d\n", *ap[2], (*pa)[2]);
    printf("pa + 1 is %td bytes further\n", (char *) (pa + 1) - (char *) pa);
    return 0;
}

Output

sizeof ap = 40, sizeof pa = 8
*ap[2] = 3, (*pa)[2] = 3
pa + 1 is 20 bytes further

A 2D array decays to a pointer to its first row, so a function receiving int m[][3] really receives int (*m)[3].

#include <stdio.h>

/* m is a pointer to arrays of 3 int – what int m[][3] means as a parameter */
static int trace(int (*m)[3], int n) {
    int t = 0;
    for (int i = 0; i < n; i++)
        t += m[i][i];
    return t;
}

int main(void) {
    int grid[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int (*row)[3] = grid;          /* grid decays to a pointer to its first row */
    printf("trace %d, row[2][0] = %d, *(*(row + 1) + 2) = %d\n", trace(grid, 3), row[2][0], *(*(row + 1) + 2));
    return 0;
}

Output

trace 15, row[2][0] = 7, *(*(row + 1) + 2) = 6

Pointers to functions

A function name converts to a pointer to the function, so op = add and op = &add are the same, and so are op(6, 7) and (*op)(6, 7). Without the parentheses, int *op(int, int) would declare a function returning int *.

#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) = add;                 /* pointer to function (int, int) returning int */
    printf("%d\n", op(6, 7));
    op = &mul;                                 /* & is optional */
    printf("%d\n", (*op)(6, 7));               /* so is * when calling */

    int (*table[2])(int, int) = {add, mul};    /* array of 2 such pointers */
    printf("%d %d\n", table[0](2, 3), table[1](2, 3));
    return 0;
}

Output

13
42
5 6

Functions that return function pointers

The parameter list of the function goes right after its name, and the return type wraps around it: int (*pick(char c))(int) is "pick is a function (char) returning a pointer to function (int) returning int".

#include <stdio.h>

int twice(int x)  { return 2 * x; }
int square(int x) { return x * x; }

/* pick: function taking char, returning pointer to function (int) returning int */
int (*pick(char c))(int) {
    return c == 's' ? square : twice;
}

int main(void) {
    printf("%d %d\n", pick('s')(9), pick('t')(9));
    return 0;
}

Output

81 18

Taming declarations with typedef

Name each layer once and the rest becomes readable. The standard library does the same: POSIX declares sighandler_t so that signal becomes sighandler_t signal(int, sighandler_t);.

#include <stdio.h>

typedef int (*unary)(int);          /* name the function-pointer type once */

int inc(int x) { return x + 1; }
int neg(int x) { return -x; }

unary pick(char c) { return c == '-' ? neg : inc; }   /* same as int (*pick(char))(int) */

int main(void) {
    unary steps[] = {inc, inc, neg, inc};
    int v = 5;
    for (int i = 0; i < 4; i++)
        v = steps[i](v);
    printf("%d %d\n", v, pick('-')(v));
    return 0;
}

Output

-6 6

Where const goes

const applies to what is on its left (or to its right if nothing is on its left). Read right to left: int *const p – "p is a const pointer to int"; const int *p – "p is a pointer to int that is const".

#include <stdio.h>

int main(void) {
    int a = 1, b = 2;
    const int *p1 = &a;        /* pointer to const int: *p1 is read-only, p1 can move */
    int *const p2 = &a;        /* const pointer to int: p2 is fixed, *p2 can change   */
    const int *const p3 = &a;  /* both fixed */

    p1 = &b;
    *p2 = 10;
    printf("%d %d %d\n", *p1, a, *p3);
    return 0;
}

Output

2 10 10

C23 typeof

typeof lets you build complicated types from simpler ones, often more readably than the classic syntax.

#include <stdio.h>

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

int main(void) {
    typeof(add) *fp = add;                 /* pointer to a function with add's type */
    typeof(int[4]) *rowp = nullptr;        /* pointer to array of 4 int */
    typeof(typeof(add) *[3]) ops = {add, add, add};
    printf("%d %zu %zu\n", fp(2, 3), sizeof *rowp, sizeof ops / sizeof ops[0]);
    return 0;
}

Output

5 16 3

Declarations C does not allow

  • A function cannot return an array or a function: int f(void)[3] and int g(void)(int) are errors – return a pointer instead.
  • There are no arrays of functions: int fs[3](int) is an error – use an array of function pointers.
  • Arrays of void do not exist.

Key points to remember

  • [] and () bind tighter than * – parentheses change the meaning.
  • Read from the name: right first, then left, inside parentheses first.
  • int *p[5] is 5 pointers; int (*p)[5] is one pointer to 5 ints.
  • const binds to what is on its left.
  • Use typedef for any declaration you need to read twice.

Frequently asked questions

Is there a tool to decode C declarations?

Yes – the classic cdecl program (also available online) translates declarations to English and back.

Why does C use such odd syntax?

"Declaration mirrors use": the declaration shows an expression and its resulting type. int (*fp)(int) says that (*fp)(x) is an int.

Where can I learn more?

Read Function Pointers in C and Pointer Arithmetic in C, then practise with the exercises below.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C Programming

C programming is the language most computer science courses start with and the foundation of operating systems, embedded devices and compilers. This section helps you learn C properly – data types, operators, control statements, functions, arrays, strings, pointers, structures, memory allocation and files – and then prove it with hundreds of practice questions explained line by line.

Why it matters

  • C teaches how a computer really works: memory, addresses and how data is stored
  • Pointers and memory questions are a favourite in written tests and technical interviews
  • Once you know C, learning C++, Java and Python becomes much easier
  • Embedded systems, operating systems and many companies' coding rounds still use C

Who it is for

  • School students (Class 11–12 computer science) building a strong programming base
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, aptitude-cum-coding tests and technical interviews
  • Anyone preparing for GATE CS or government IT exams

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro tests
  • Technical interviews
  • GATE CS
  • University exams
  • Government IT exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links