450 views
9 9 votes

Consider the following code idea from the source:

typedef struct {
    double x, y;
} Point;

void reset(Point p) {
    p.x = p.y = 0;
}

int main() {
    Point a = {12.0, 42.0};
    Point b = a;

    reset(a);
    b.x = 0;

    printf("a: %.0f,%.0f\n", a.x, a.y);
    printf("b: %.0f,%.0f\n", b.x, b.y);
}

What is the output?

A.

a: 0,0
b: 0,42

B.

a: 12,42
b: 0,42

C.

a: 12,42
b: 12,42

D.

a: 0,42
b: 0,42

1 Answer

2 2 votes

$\texttt{Point b = a;}$ copies the structure values. 

Then $\texttt{reset(a)}$ gets a copy of $\texttt{a}$, so the original $\texttt{a}$ is not changed. 

After that, $\texttt{b.x = 0}$ changes only $\texttt{b.x}$.

So $\texttt{a}$ remains $\texttt{(12, 42)}$ and $\texttt{​​​​​​​b}$ becomes $\texttt{(0, 42)}$.

Answer: B

Answer:
Position:
Show:

Related questions

13 13 votes
1 1 answer
1.1k
1.1k views
GO Classes asked Jul 2
1,134 views
Assume:$\texttt{short = 2 bytes}$, $\texttt{int = 4 bytes}$, $\texttt{char = 1 byte}$$\texttt{int}$ needs $4$-byte alignment and the final structure size is rounded to a ...
15 15 votes
2 2 answers
600
600 views
GO Classes asked Jul 2
600 views
Assume:struct Student { double gpa; }; struct Student alice; struct Student *sptr = &alice;Which of the following correctly assigns $\texttt{4.0}$ to $\texttt{alice.gpa}$...
6 6 votes
1 1 answer
431
431 views
GO Classes asked Jul 2
431 views
What is printed by the following program?#include <stdio.h struct Student { int id, year; char grade; }; int main() { struct Student s; s.id = 10001; s.year = 2010; s.gra...
8 8 votes
1 1 answer
402
402 views
GO Classes asked Jul 2
402 views
Which option correctly completes the given exercise?#include <stdio.h /* define the person struct here using the typedef syntax */ int main() { person john; john.name = "...