fork download
  1. #include <stdio.h>
  2.  
  3. // 構造体の定義
  4. struct student {
  5. int id; /* 学籍番号 */
  6. int eng; /* 英語の成績 */
  7. int math; /* 数学の成績 */
  8. int sci; /* 理科の成績 */
  9. int total; /* 合計点 */
  10. };
  11.  
  12. // 値渡し関数(スライド通り)
  13. void sum1(struct student s) {
  14. s.total = s.eng + s.math + s.sci;
  15. }
  16.  
  17. // 参照渡し関数(★間接演算子をアロー演算子に書き換え箇所)
  18. void sum2(struct student *p) {
  19. p->total = p->eng + p->math + p->sci;
  20. }
  21.  
  22. int main() {
  23. // 構造体変数の初期化
  24. struct student s = {17001, 60, 100, 20};
  25.  
  26. // sum1(値渡し)の呼び出しと結果出力
  27. sum1(s); /* 値渡し */
  28. printf("%7d %5d %5d %5d %10d\n", s.id, s.eng, s.math, s.sci, s.total);
  29.  
  30. // sum2(参照渡し・アロー演算子版)の呼び出しと結果出力
  31. sum2(&s); /* 参照渡し */
  32. printf("%7d %5d %5d %5d %10d\n", s.id, s.eng, s.math, s.sci, s.total);
  33.  
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
  17001    60   100    20          0
  17001    60   100    20        180