Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. /* Example: StructuresIndirect in CodeBlocks 2. structures as function argume

ID: 3698695 • Letter: 1

Question

1. /* Example: StructuresIndirect in CodeBlocks

2. structures as function arguments and return values

3. indirect instances using pointers */

4.

5. #include <stdio.h>

6. #include <time.h>

7. #include <stdlib.h>

8.

9. struct cplx

10. {

11. double real; /* real part */

12. double imag; /* imaginary part */

13. };

14.

15. struct cplx add(struct cplx* pa, struct cplx* pb); /* function prototype */

16.

17. int main(void)

18. {

19. struct cplx z;

20. time_t rawtime = time(NULL);

21.

22.

23. struct cplx* px= malloc(sizeof(struct cplx));

24. struct cplx* py= malloc(sizeof(struct cplx));

25.

26. px->real = 2.5;

27. px->imag = 5.0;

28. py->real = 3.2;

29. py->imag = -1.7;

30.

31.

32. z = add(px, py);

33. printf("Indirect Instance z = %4.2f + %4.2f j ", z.real, z.imag);

34. printf("Today is %s", ctime(&rawtime));

35.

36. return 0;

37. }

38.

39.

40. struct cplx add(struct cplx* pa, struct cplx* pb)

41. {

42. struct cplx c = *pa; /* can initialise an auto struct variable */

43.

44. c.real += pb->real;

45. c.imag += pb->imag;

46. return c; /* can return a struct value */

47. }

11. Which lines create instances of the structure, what are the structure names?

12. What are the structure pointer names?

13. Which lines initialize the member elements of the structures? How?

14. Which line is the prototype for a function with structure pointer parameters

15. Which lines are the function definition with structure pointer parameters

16. How are the member elements of an unamed (indirect instance) structure dereferenced (how are the values changed)?

17. How is the addition of two structures accomplished?

18. Could a pointer be returned from the function?

Explanation / Answer


11. Which lines create instances of the structure, what are the structure names?
=> line 19. struct cplx z;

12. What are the structure pointer names?
=> names px and py
23. struct cplx* px= malloc(sizeof(struct cplx));
24. struct cplx* py= malloc(sizeof(struct cplx));


13. Which lines initialize the member elements of the structures?
=>
26. px->real = 2.5;
27. px->imag = 5.0;
28. py->real = 3.2;
29. py->imag = -1.7;

14. Which line is the prototype for a function with structure pointer parameters ?
=>
15. struct cplx add(struct cplx* pa, struct cplx* pb); /* function prototype */


15. Which lines are the function definition with structure pointer parameters?
=>
40. struct cplx add(struct cplx* pa, struct cplx* pb)


16. How are the member elements of an unamed (indirect instance) structure dereferenced (how are the values changed)?
=> using mutuators functions like setters the values can be changed

17. How is the addition of two structures accomplished?
=> It is accomplished by adding real values of compelex numbers
and imaginary vlaues of both complex numbers. Since this is a complex number structre

18. Could a pointer be returned from the function?
=> yes, if the accepted value in function call is pointer then a pointer can be returned from the function