friends.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* friends.c -- uses pointer to a structure */
  2. #include <stdio.h>
  3. #define LEN 20
  4. struct names {
  5. char first[LEN];
  6. char last[LEN];
  7. };
  8. struct guy {
  9. struct names handle;
  10. char favfood[LEN];
  11. char job[LEN];
  12. float income;
  13. };
  14. int main(void)
  15. {
  16. struct guy fellow[2] = {
  17. {{ "Ewen", "Villard"},
  18. "grilled salmon",
  19. "personality coach",
  20. 68112.00
  21. },
  22. {{"Rodney", "Swillbelly"},
  23. "tripe",
  24. "tabloid editor",
  25. 432400.00
  26. }
  27. };
  28. struct guy * him; /* here is a pointer to a structure */
  29. printf("address #1: %p #2: %p\n", &fellow[0], &fellow[1]);
  30. him = &fellow[0]; /* tell the pointer where to point */
  31. printf("pointer #1: %p #2: %p\n", him, him + 1);
  32. printf("him->income is $%.2f: (*him).income is $%.2f\n",
  33. him->income, (*him).income);
  34. him++; /* point to the next structure */
  35. printf("him->favfood is %s: him->handle.last is %s\n",
  36. him->favfood, him->handle.last);
  37. return 0;
  38. }