Function Pointers, Callbacks & String Memory Layouts
“Function pointer syntax int (*fp)(int), callbacks, and mutable char[] vs immutable char*.”
1.Function Pointers & Callback Architecture
§ 2.1.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[]
§ 2.2.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)!
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 for clarity, rigorous numerical solving, and semester excellence:
Systematic beginner-to-advanced C with clear examples.
Clear step-by-step C explanations — great for semester lab preparation.