Skip to content
Physics and Astronomy
Home Our Teaching Resources C programming Common mistakes
Back to top

Common mistakes with structures

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
                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Privacy & cookies Share
Back to top