「配列で実現する文字列」の配列と、「ポインタで実現する文字列」の配列
明解C言語 入門編 > 11. 文字列とポインタ >
「配列で実現する文字列」の配列と、「ポインタで実現する文字列」の配列
C
#include <stdio.h>int main(int argc, char* argv[])
{
char str[3][6] = {"Turbo", "NA", "DOHC"};
char* ptr[3] = {"12345", "67", "890A"};int i;
for (i = 0; i < 3; i++)
printf("str[%d] = \"%s\"\n", i, str[i]);puts("");
for (i = 0; i < 3; i++)
printf("ptr[%d] = \"%s\"\n", i, ptr[i]);return 0;
}
実行結果
R:\>lesson083\project1.exe
str[0] = "Turbo"
str[1] = "NA"
str[2] = "DOHC"ptr[0] = "12345"
ptr[1] = "67"
ptr[2] = "890A"
Delphi
program Project1;{$APPTYPE CONSOLE}
uses
SysUtils;procedure main();
const
str: array[1..3] of String = ('Turbo', 'NA', 'DOHC');
ptr: array[1..3] of array[0..255] of Char = ('12345', '67', '890A');
var
i: Integer;
begin
for i := 1 to 3 do
Writeln(Format('str[%d] = ''%s''', [i, str[i]]));Writeln('');
for i := 1 to 3 do
Writeln(Format('ptr[%d] = ''%s''', [i, ptr[i]]));
end;begin
main;
end.
実行結果
S:\>lesson083\project1.exe
str[1] = 'Turbo'
str[2] = 'NA'
str[3] = 'DOHC'ptr[1] = '12345'
ptr[2] = '67'
ptr[3] = '890A'