Common mistakes with structures
Comments and questions to John Rowe.
Wrong
| Right
|
float area(float axes[]) {
return PI * axes[0] * axes[1];
}
// Inside another function
el->area = area(el->axes);
|
void calculate_area(Ellipse *e) {
e->area = PI * e->axes[0] * e->axes[1];
}
// Inside another function
calculate_area(el);
|
In the incorrect example on the left we are passing
part of a structure (in this case the address of an array)
to the function. We should be passing a pointer to the whole structure.
Log in