Entrance Exams
Govt. Exams
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'.
Total elements = 2 × 3 × 4 = 24 elements in the 3D array.
char str[6] = {'H','e','l','l','o'};
printf("%s", str);
The array has 5 characters but is not null-terminated. printf("%s") will read beyond the array, printing garbage.
arr[0] points to the first row (array of 4 ints). arr[0][0] and *(*arr) both access the first element through pointer dereferencing.
arr[5] accesses the element at index 5 (6th element), while *arr (equivalent to arr[0]) accesses the first element.
int arr[5]; for(int i=0; i
Loop runs from i=0 to i=4, executing 5 times total. All array elements are initialized to 0.
char str[] = "Hello";
printf("%c", *(str+2));
str+2 points to the 3rd character (index 2). *(str+2) dereferences it, printing 'l'.