What is the difference between *p++ and (*p)++?
Due to operator precedence, *p++ is equivalent to *(p++), incrementing the pointer. (*p)++ explicitly increments the value.
What does the following code do?
int *p = (int*)malloc(sizeof(int));
*p = 5;
free(p);
p = NULL;
This is proper memory management: allocate, use, free, and nullify to prevent dangling pointer.
Which statement is true about void pointers?
void pointers are generic pointers that must be cast to appropriate type before dereferencing.
What is the output?
int x = 50;
int *p = &x;
int *q = p;
q = NULL;
printf("%d", *p);
Setting q to NULL doesn't affect p. p still points to x, so *p is 50.
In a function that returns a pointer, which of the following is UNSAFE?
Returning a pointer to a local variable is unsafe because the variable ceases to exist after the function returns, creating a dangling pointer.
Advertisement
Consider: int *p = (int *)malloc(5 * sizeof(int)); for(int i=0; i<5; i++) *(p+i) = i*10; Which statement correctly deallocates this memory?
malloc() allocated memory must be freed using free(p), not free(&p). After freeing, it's good practice to set p = NULL.
A programmer uses a pointer variable but forgets to initialize it before dereferencing. Which type of error will occur?
Uninitialized pointers contain garbage values. Dereferencing them accesses arbitrary memory locations, causing runtime errors or undefined behavior.
What is the relationship between arrays and pointers in C?
In most contexts, array names automatically decay to pointers to their first element. This is why array[i] is equivalent to *(array+i).
What will be the output?
int arr[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
int *p = (int *)arr;
printf("%d", *(p+5));
Converting 2D array to int pointer treats it as 1D. p+5 points to the 6th element (0-indexed), which is 6.
What does realloc() do?
realloc() changes the size of previously allocated memory. It may return the same address or a new one depending on availability.
What is the primary use of const pointer (int * const p)?
int * const p is a constant pointer - the pointer address cannot be changed, but the value it points to can be modified.
Consider: int arr[] = {1,2,3,4,5}; int *p = arr + 2; What is arr - p?
arr points to arr[0], p points to arr[2]. arr - p = -2 (p is 2 positions ahead).
What is malloc(0) likely to return?
malloc(0) behavior is implementation-defined but typically returns a valid pointer. Freeing it is safe.
What will be printed?
int *p = (int*)malloc(5 * sizeof(int));
int *q = p;
p = NULL;
free(q);
q still holds the address even though p is NULL. free(q) properly deallocates memory.
What is the difference between arr and &arr if arr is an array?
int arr[5];
arr decays to pointer to first element (int*). &arr is pointer to whole array (int(*)[5]). Pointer arithmetic differs.
In pointer to function: int (*ptr)(int, int); What does this declare?
Parentheses around *ptr give pointer priority. It's a pointer to a function taking 2 ints and returning int.
What is the output of: printf("%p", NULL);?
%p prints pointer values. NULL representation depends on implementation, commonly shown as 0x0 or (nil).
For dynamic 2D array: int arr = (int)malloc(n * sizeof(int*)); What's missing?
This allocates row pointers only. Each row needs allocation: arr[i] = (int*)malloc(m * sizeof(int));
What is the relationship between arrays and pointers?
Array name acts as pointer to first element in expressions but they're distinct. &arr and arr differ in type.
Consider: const int *p; and int * const q; Which statement is true?
const int *p: pointer can change, data cannot. int * const q: pointer cannot change, data can be modified.