文字列の配列を受け渡し
明解C言語 入門編 > 9. 文字列の基本 >
文字列の配列を受け渡し
C
#include <stdio.h>void put_strary(const char st[][6], int n)
{
int i;
for (i = 0; i < n; i++)
{
int j = 0;
printf("st[%d] = \"", i);while (st[i][j])
putchar(st[i][j++]);puts("\"");
}
}int main(int argc, char* argv[])
{
char cs[][6] = {"Turbo", "NA", "DOHC"};
put_strary(cs, 3);return 0;
}
実行結果
R:\>lesson075\Project1.exe
st[0] = "Turbo"
st[1] = "NA"
st[2] = "DOHC"
Delphi
program Project1;{$APPTYPE CONSOLE}
uses
SysUtils;procedure put_strary(st:array of String);
var
i, j: Integer;
begin
for i := Low(st) to High(st) do
begin
write(Format('st[%d] = ''', [i]));for j := 1 to Length(st[i]) do
write(Format('%s', [st[i][j]]));Writeln('''');
end;
end;procedure main();
var
cs: array[1..3] of String;
i: Integer;
begin
cs[1] := 'Turbo';
cs[2] := 'NA';
cs[3] := 'DOHC';
put_strary(cs);
end;begin
main;
end.
実行結果
S:\>lesson075\Project1.exe
st[0] = 'Turbo'
st[1] = 'NA'
st[2] = 'DOHC'