Unit V · Viva Questions
Revise the concepts orally. Open the study answer for a detailed explanation.
Define a pointer and explain the address and dereference operators.
- Does int *p automatically create an int for p to point at?
- No. It declares the pointer object only.
- Does *p = 25 change p itself?
- No. It changes the object reached through p.
Explain pointer types, void pointers and null pointers.
- Is arithmetic on void * part of standard C?
- No.
- Does every pointer occupy four bytes?
- No. The size depends on its type and implementation.
Explain how pointer arguments allow a function to modify caller data.
- What does assigning to a pointer parameter itself change?
- Only that parameter object unless another object is explicitly reached through it.
- Does p != NULL prove that dereferencing p is safe?
- No. Its target must also be valid, live and suitable.
Explain the relationship between pointers and one-dimensional arrays.
- Can an ordinary array name be incremented using a++?
- No. It is not a modifiable pointer variable.
- Is sizeof a always the pointer size?
- No. For the actual array object it is the whole array size; an adjusted array parameter is different.
Explain valid pointer arithmetic and pointer subtraction.
- What type holds a pointer difference?
- ptrdiff_t, declared by stddef.h.
- May the one-past-end pointer be read through *?
- No.
Explain an array of pointers with a string example.
- Are the characters of all strings necessarily contiguous together?
- No. The pointer elements are contiguous, but their targets can be separate.
- Can the program modify a string literal through these pointers?
- No.
Explain pointers to pointers with an example.
- Does redirect change the first integer in this example?
- No. It changes the pointer p to identify the second integer.
- What address is passed to an int ** parameter to update p?
- &p, when p has type int *.
Explain function pointers and callback-style use.
- Can every function be called through one arbitrary function-pointer type?
- No. The called function type must be compatible.
- What is a callback?
- A function supplied to another operation so that it can be called as part of that operation.
Explain pointers to rows of a two-dimensional array.
- How far does p + 1 step for int (*p)[3]?
- By one complete row of three ints.
- Does casting a matrix to int ** create row pointers?
- No. A cast does not change its physical object layout.
Explain dynamic memory allocation and its main functions.
- Does a local pointer going out of scope automatically free its allocation?
- No.
- Is a cast required for malloc’s result in C?
- No, when assigning its void * result to a suitable object pointer.
Compare malloc and calloc with a complete example.
- May uninitialised malloc memory be assumed to contain zero?
- No.
- Is free(NULL) permitted?
- Yes. It has no effect.
Explain safe use of realloc and free with an example.
- Does realloc clear all newly added bytes?
- No.
- Does setting one freed pointer to NULL fix every alias?
- No. Other pointers to the old block remain invalid.
Explain C strings, null termination and character arrays.
- Is an empty string stored in zero array positions?
- No. It still needs a position for its terminator.
- Does sizeof word equal strlen(word) for char word[] = "Code"?
- No. sizeof is 5, while strlen is 4.
Explain whole-line string input using fgets and safe newline handling.
- Does fgets always remove the newline?
- No. It stores it when read; the program can remove it deliberately.
- Why can an exactly full stored line still be valid?
- The next input character may be its newline, or input may have ended.
Explain the main string library functions and their precautions.
- Does strcpy know the destination array capacity?
- No. The caller must ensure enough space.
- Does strcmp promise to return exactly -1 for a smaller string?
- No. Only the sign is specified.
Explain string initialisation, pointer assignment and copying into a character array.
- Does pointer assignment duplicate string storage?
- No.
- Why must source length be smaller than destination capacity?
- The extra position is needed for the null terminator.
Write a C function to extract a substring from a given position.
- Does the copied substring need its own terminator?
- Yes.
- What index convention is used here?
- Zero-based positions, matching C array indices.
Explain string concatenation with a capacity-checked example.
- Does strcat allocate a larger destination?
- No.
- Why can repeated concatenation become costly?
- Each call may scan the growing destination again.
Explain string comparison using strcmp and why == is unsuitable for contents.
- Can two distinct arrays hold equal strings?
- Yes. strcmp can return zero even when they are different objects.
- Is strcmp automatically case-insensitive?
- No.
Explain character pointers and const string parameters in functions.
- Can const char * still be redirected as a local parameter?
- Yes. The characters are const-qualified through it, not necessarily the pointer object.
- Is returning a pointer to a local automatic char array safe?
- No. The array’s lifetime ends when that block execution ends.
Write a function to find a string’s length without strlen.
- What length does the empty string have?
- Zero.
- Why is a null pointer not accepted as an empty string here?
- The function needs actual accessible storage containing the terminator.
Write a function to insert a substring at a given string position.
- Is inserting at position equal to length valid?
- Yes. It appends the inserted text.
- Why is the terminator moved with the suffix?
- The final result must remain a valid terminated string.
Write a function to delete n characters from a given string position.
- Does deletion require a larger destination buffer?
- No. The string becomes shorter or remains unchanged.
- What remains after deleting all characters?
- A valid empty string beginning with a null terminator.
Explain uninitialised pointers, dangling pointers and memory leaks.
- Can a dangling pointer still be non-null?
- Yes.
- Does a cast allocate new storage or extend an object’s lifetime?
- No.