ProgrammingHardProduct company OA
Why is memcmp(&a, &b, sizeof a) == 0 an unreliable way to test whether two struct variables a and b have equal members?
- A. Padding bytes between or after members may hold different values even when every member is equal
- B. memcmp compares only the first member of a struct
- C. Struct addresses cannot be passed to memcmp
- D. memcmp stops comparing at the first zero byte
Correct answer: A. Padding bytes between or after members may hold different values even when every member is equal
memcmp compares raw bytes, including any padding the compiler inserted, whose contents are unspecified. Two structs with equal members can therefore compare unequal. (Floating-point members add more traps, such as 0.0 and −0.0.) memcmp does not stop at zero bytes; that is strcmp.
More C Programming questions
- What is printed? c int c = 0; int f(int n) { c++; return n > 0 && (f(n - 1) || f(n - 2)); } int main(void) { int r = f(4); printf("%d %d"…
- What is printed? c char buf[32], p = buf; for (int i = 1; i <= 4; i++) p += sprintf(p, "%d,", i i); p[-1] = '0'; printf("%s %td", buf, p…
- How many swaps does this selection sort perform? c int a[] = {3, 1, 2, 5, 4}, sw = 0; for (int i = 0; i < 4; i++) { int m = i; for (int j…
- How many element shifts (executions of a[j + 1] = a[j]) does this insertion sort make? c int a[] = {4, 3, 2, 1}, shifts = 0; for (int i =…
- How many passes of the outer loop run? c int a[] = {2, 3, 4, 5, 1}, passes = 0; for (int sw = 1; sw; ) { sw = 0; passes++; for (int j = 0…
- What is printed? c int m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { int t =…