Entrance Exams
Govt. Exams
Pointer arithmetic increments by the size of the data type. For int (4 bytes), ptr++ moves to the next integer, which is arr[1].
A void pointer cannot be directly dereferenced. It must be cast to the appropriate pointer type first.
int arr[] = {10, 20, 30};
int *p = arr;
printf("%d %d", arr[1], *(p+1));
arr[1] and *(p+1) both access the second element of the array, which is 20.
A dangling pointer is a pointer that points to memory that has been freed or deallocated.
malloc requires the size in bytes. For 10 integers, we need 10 * sizeof(int) bytes.
int *p = NULL;
if(p) printf("Not NULL");
else printf("NULL");
NULL pointer evaluates to false in a conditional, so the else block executes.
A wild pointer is an uninitialized pointer that contains an arbitrary/garbage memory address.
int arr[] = {1, 2, 3};
int *p = arr;
printf("%d", *(p+2));
p+2 points to the third element of the array. *(p+2) dereferences it to get 3.
int x = 10, y = 20;
int *p = &x;
int *q = &y;
p = q;
printf("%d", *p);
After p = q, pointer p now points to y. So *p gives the value of y, which is 20.
int arr[5] = {1,2,3,4,5};
int *p = arr;
p++;
When a pointer is incremented, it moves to the next element based on the data type size (pointer arithmetic).