PROGRAMMING IN C/Student Engineering Notebook
Page 04·notes
POINTERS, ARRAY DECAY & FUNCTION POINTERS—~28 MIN READ

Function Pointers, Callbacks & String Memory Layouts

“Function pointer syntax int (*fp)(int), callbacks, and mutable char[] vs immutable char*.”

1.Function Pointers & Callback Architecture

In C, code resides in the Text / Code Segment. Every function has an entry-point memory address that can be stored in a function pointer.

Declaration Syntax:

return_type (*pointer_name)(param_types);

- Example: int (*fp)(int, int); declares fp as a pointer to a function taking two int parameters and returning an int.

- Note: int fp(int, int); WITHOUT parentheses declares a function returning an int!

Invocation:

If int add(int a, int b) { return a + b; }, then fp = add; or fp = &add;. Invoking fp(3, 4) or (*fp)(3, 4) both return 7.

2.String Literal Immutability: char *s vs char s[]

Strings in C are null-terminated ('\0') character sequences:

1. char s[] = "GATE";:

- Allocates an array of 5 characters ('G', 'A', 'T', 'E', '\0') on the Stack.

- The contents are MUTABLE: s[0] = 'L'; modifies the string to "LATE" safely.

2. char *s = "GATE";:

- Stores the string literal in the Read-Only Data Segment (.rodata).

- The pointer s points to read-only memory.

- Attempting s[0] = 'L'; causes a Segmentation Fault / Bus Error (Undefined Behavior)!

SOURCE TRANSPARENCY & CITATIONS

Primary Academic References & Exam Sources

Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.

Curated University Lecture Series
The C Programming Language (2nd Edition) ·
LECTURE SERIES
RECOMMENDED YOUTUBE EDUCATORS & FULL-COURSE PLAYLISTS

Curated for clarity, rigorous numerical solving, and semester excellence:

Neso Academy · C Programming Full Course↗Instructor: Neso Academy · Covers: Data types, pointers, arrays, functions, recursion, file I/O

Systematic beginner-to-advanced C with clear examples.

Open YouTube↗
Jenny's Lectures CS/IT · C Programming↗Instructor: Jenny's Lectures · Covers: Arrays, strings, pointers, structures, file handling, preprocessor

Clear step-by-step C explanations — great for semester lab preparation.

Open YouTube↗