fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main ()
  5. {
  6. // The nested structure within myStruct will hold an array of 2 ints
  7. typedef struct{
  8. int Vals[2];
  9. } nestedStruct;
  10. // myStruct is a struct typedef that has 2 fields
  11. typedef struct{
  12. int AnInt;
  13. float AFloat;
  14. nestedStruct S;
  15. } myStruct;
  16. // make an instance of myStruct called M using the typedef
  17. myStruct M[3] = {{1, 1.5f, 10, 20}, {2, 2.5f, 30, 40}, {3, 3.5f, 50, 60}};
  18. int index, subindex;
  19. for (index = 0; index < 3; index++) {
  20. printf ("M[%d] has values:\n AnInt=%d\n AFloat=%f\n",
  21. index, M[index].AnInt, M[index].AFloat);
  22. printf (" The nested structure contains: ");
  23. for (subindex = 0; subindex < 2; subindex++) {
  24. printf ("%d ", M[index].S.Vals[subindex]);
  25. }
  26. printf ("\n");
  27. }
  28. return 0;
  29. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
M[0] has values:
 AnInt=1
 AFloat=1.500000
 The nested structure contains: 10 20 
M[1] has values:
 AnInt=2
 AFloat=2.500000
 The nested structure contains: 30 40 
M[2] has values:
 AnInt=3
 AFloat=3.500000
 The nested structure contains: 50 60