Govt Exams
realloc() changes the size of previously allocated memory. It may return the same address or a new one depending on availability.
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.
In most contexts, array names automatically decay to pointers to their first element. This is why array[i] is equivalent to *(array+i).
char *str = "Hello";
printf("%c", *(str+1));
str points to 'H', str+1 points to 'e'. Dereferencing gives 'e'.
Calling free() on already freed memory or using a pointer after freeing it causes double-free errors and use-after-free bugs, leading to undefined behavior.
int x = 100, y = 200;
int *p = &x;
int *q = &y;
p = q;
printf("%d", *p);
p is reassigned to point to y (p = q). Dereferencing p now gives the value of y, which is 200.
int **p declares a pointer to a pointer (double pointer). Each * adds one level of indirection.
int *p, *q;
int x = 5;
p = &x;
q = p;
printf("%d %d", *p, *q);
Both p and q point to the same variable x. Dereferencing both gives the value 5.
calloc() takes two parameters (number of elements, size) and initializes all bytes to zero, while malloc() leaves memory uninitialized.
int a[] = {10, 20, 30};
int *p = a;
printf("%d", *(p+2));
p points to a[0], p+2 points to a[2] which contains 30. Dereferencing gives 30.