構造体とメンバの透過性
構造体とメンバの透過性
C++
#include <stdio.h>struct student
{
char name[20];
int eng;
int math;
int ave;
};void calc_ave(student* x)
{
x->ave = (x->eng + x->math) / 2;
}int average(student* x)
{
return x->ave;
}int main(int argc, char* argv[])
{
student x;x.eng = 60;
x.math = 70;calc_ave(&x);
printf("eng = %d\n", x.eng);
printf("math = %d\n", x.math);
printf("ave = %d\n", average(&x));return 0;
}
実行結果
R:\>lesson002\project1.exe
eng = 60
math = 70
ave = 65
Delphi
program Project1;{$APPTYPE CONSOLE}
uses
SysUtils;type
TStudent = record
eng: Integer;
math: Integer;
ave: Integer;
end;
procedure calc_ave(var x:TStudent);
begin
x.ave := (x.eng + x.math) div 2;
end;function average(x:TStudent):Integer;
begin
result := x.ave;
end;procedure main();
var
x: TStudent;
begin
x.eng := 60;
x.math := 70;calc_ave(x);
Writeln(Format('eng = %d', [x.eng]));
Writeln(Format('math = %d', [x.math]));
Writeln(Format('ave = %d', [average(x)]));
end;begin
main;
end.
実行結果
S:\>lesson002\project1.exe
eng = 60
math = 70
ave = 65