Character Pointer and String Interview Questions in C: Answers with Memory Traces

Prepare clear C interview answers about character pointers, strings, array decay, const, lifetime, capacity, and operator precedence using exact memory traces.

KnowledgeGate Team

Exam prep & CS education

Updated 21 Nov 20265 min read
Character Pointer and String Interview Questions in C: Answers with Memory Traces

In a short C program, char *p, char text[], a string literal, and a null terminator can look interchangeable. They differ in ownership, modifiability, lifetime, and what sizeof measures. In a writable array, word changes from CODE to COAE at pointer offset 1, with lengths 4 and 3; operator precedence changes abc to acc.

Character pointers and C strings: the precise interview definition

What is a string in C? A C string is a sequence of char values terminated by the first \0; C has no separate built-in string type. char text[] = "CAT"; has four elements: 'C', 'A', 'T', and '\0'. Thus strlen(text) is 3, while sizeof text is 4 in the declaring scope.

Declaration

What exists

Characters changeable?

Pointer retargetable?

char text[] = "CAT";

A writable four-element array

Yes

No pointer exists

const char *view = "CAT";

A pointer to a literal

Not through view

Yes

char *cursor = text;

A pointer into the writable array

Yes

Yes

An array may decay to a pointer but is not one. See Strings in C: Declaration, Input & Worked Examples for declaration and input foundations.

Character-pointer memory trace: work CODE into COAE

Consider a writable array:

c
char word[] = "CODE";
char *p = word + 1;
p[1] = 'A';

word[0] through word[4], at conceptual addresses 1000 through 1004, hold 'C', 'O', 'D', 'E', and '\0'. p stores 1001 and points at 'O'.

p[1] is *(p + 1), one char step to 1002 (word[2]). Replacing 'D' with 'A' makes "COAE".

Afterwards, *p is 'O' and p - word is 1 element. word prints COAE; p from word[1] prints OAE. Terminator distances give strlen(word) = 4 and strlen(p) = 3. Five cells give sizeof word = 5.

The addresses are invented; one-byte increments follow sizeof(char) = 1. Mutation is valid because word is an array copy, not a literal.

Diagram of a five-cell character array before and after p[1] changes D to A, with pointer offset, string lengths, and the terminator.

String literals, modifiability, and lifetime: three model answers

Can I modify a string through char *p? Only if its target is writable. char a[] = "CAT"; a[0] = 'B'; validly produces "BAT". char *p = "CAT"; p[0] = 'B'; tries to modify a literal, causing undefined behaviour. Otherwise, use const char *p = "CAT";.

How long does the pointed string live? It follows the target object. A literal has static storage duration, so const char *label(void) { return "OK"; } may return its pointer. Automatic char temp[] = "OK"; expires when its function returns; returning temp leaves a dangling pointer. A pointer never extends its target's lifetime.

Does const char *p make the pointer constant? No. It prevents modification through p, not retargeting. char *const p = buffer cannot be retargeted. const char *const p = "CAT" permits neither operation.

sizeof, strlen, NULL, and array decay: exact output answers

Assume a 64-bit platform with sizeof(char *) = 8, then declare char desk[] = "desk"; const char *view = desk;. The elements d, e, s, k, and \0 give sizeof desk = 5 and strlen(desk) = 4. Because view is a pointer, sizeof view = 8; its four visible characters give strlen(view) = 4. Pointer size is implementation-dependent, while sizeof(char) is always 1.

void inspect(char s[]) adjusts the parameter to char *s. Under our assumption, sizeof s is 8, not the caller's array capacity. Pass length or capacity explicitly. Arrays and Strings in C: Decay, sizeof and 2D Addresses develops this rule.

NULL represents a null pointer constant; \0 is the integer-zero character terminating a C string. A null pointer targets no object. A pointer to "" is non-null and targets an array beginning with \0.

Pointer-operator precedence: trace abc into acc

Parse each expression before assigning its value:

c
char s[] = "abc";
char *p = s;
char first = *p++;
char old = (*p)++;
char now = *p;

Postfix ++ binds more tightly than unary *, so *p++ means *(p++). It stores 'a' in first, then moves p from s[0] to s[1]; the array stays "abc".

Parentheses change the next operation. (*p)++ stores the old value 'b' in old, then increments it to 'c' without moving p. The array becomes "acc", p remains at s[1], now = *p stores 'c', and p - s = 1.

Checksum: first = 'a', old = 'b', now = 'c', s = "acc", pointer offset 1.

Independent expression

Parse and effect

*p++

*(p++): dereference, then advance the pointer

(*p)++

Increment the pointed character; do not move the pointer

*++p

Advance the pointer first, then dereference it

Each row is independent.

Timeline showing pointer increment and pointed-character increment across a, b, c, ending with s = "acc" and p-s = 1.

Safe string-function design: ownership, capacity, and termination

What should a safe copy function receive? int copy_text(char *dst, size_t capacity, const char *src) accepts writable dst, read-only src, and capacity including \0. Failure must be defined so dst never overflows.

With char dst[6], "CODE" has strlen(src) = 4, needs 4 + 1 = 5 bytes, and fits. "POINTER" has strlen(src) = 7 and needs 7 + 1 = 8 bytes, exceeding six. The function fails without changing dst; it truncates only if its contract promises that.

Check destination writability, capacity, lifetimes, terminator space, overlap policy, and returned-pointer lifetime. For char *dst, sizeof dst reports pointer size, not capacity.

Character-pointer interview traps and how the questioning deepens

Tempting answer

What goes wrong

Correct answer

Array equals pointer

Object and conversion differ

An array may decay, but is not a pointer

Literal is writable

Modification is undefined

Use const char *

Allocate strlen(src)

No room for \0

Add one byte

Compare text with ==

It compares addresses

Compare characters

Return a local array

It expires

Return longer-lived storage

Parameter sizeof gives capacity

It measures a pointer

Pass capacity

NULL terminates a string

Pointer and character differ

Use \0

Interview questioning can start with definitions and output, then combine sizeof versus strlen, literal safety, const-correct APIs, ownership, lifetime, capacity, undefined behaviour, and edge cases. Before predicting output, name the object, pointer, terminator, and lifetime.

The Character Pointer (String) path offers practice questions. Practise with Character Pointer (String) MCQs in C: 12 Solved, then use Placement Preparation for wider preparation. Use the set to test this topic, not to predict an employer's frequency.

Character pointers and strings: the 30-second answer and next step

A character pointer stores an address; a C string is a null-terminated character sequence. Arrays own elements; pointers own addresses. Never modify string literals. strlen counts characters before \0, sizeof measures type and object size, and pointer arithmetic scales by the pointed type.

Reproduce CODE to COAE: word = "COAE", p = "OAE", lengths 4 and 3. Then trace abc to acc, with first = 'a', old = 'b', and offset 1. C Language, Concepts, MCQs, Coding Questions provides the structured next step.