Govt Exams
C standard defines arr[i] as *(arr + i). Both notations access the same memory location and perform identically.
memcpy() is the most efficient as it performs block memory copy. strcpy() is only for strings, and array assignment doesn't work in C.
For dynamic allocation, use double pointer char** and allocate memory for each string separately. Options B and C are static allocations.
int arr[3] = {10, 20, 30};
int *p = arr;
printf("%d %d", *p++, *++p);
This code exhibits undefined behavior due to lack of sequence points between modifications and access of p. Different compilers may produce different results.
char *ptr;
char str[] = "Programming";
ptr = str;
ptr[2] = 'X';
ptr points to the character array str. Modifying ptr[2] modifies str[2], changing 'o' to 'X', resulting in 'PrXgramming'.
char arr[5] = "Hello";
printf("%d", strlen(arr));
Array is too small. "Hello" needs 6 bytes including null terminator, but only 5 are allocated. This causes buffer overflow.
After allocating the array of pointers, each row must be allocated separately.
int *arr[10] declares an array of 10 pointers to int (subscript binds tighter than dereference). Use int (*arr)[10] for pointer to array.
strncpy with buffer size check and explicit null termination is the standard safe approach. Option A lacks bounds checking; C and D have issues with null termination.
ptr[3] is equivalent to arr[3] and accesses the element at address *(ptr+3). For int*, ptr+3 moves 12 bytes (3×4 bytes) in memory.