Entrance Exams
Govt. Exams
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.
Array names are pointer constants that point to the first element and cannot be reassigned, while pointer variables can be modified to point to different addresses.
The %p format specifier prints the pointer value as a memory address in hexadecimal format, not the dereferenced value.
Since an array pointer is passed, the function can access and modify the original array elements in the caller's scope.
To modify a variable in the calling function, you must pass its address (pointer). Pass by value creates a copy and cannot modify the original.
p points to arr[0]. p+2 points to arr[2], and *(p+2) dereferences to the value at arr[2], which is 3.
malloc() and realloc() return uninitialized memory with garbage values. calloc() initializes all bytes to zero.
q is a pointer to pointer p. **q dereferences twice: first to get p, then to get the value x, which is 5.
A dangling pointer occurs when it points to memory that no longer exists, typically after free() or when a local variable goes out of scope.