配列とポインタ

明解C言語 入門編 > 10. ポインタ >

配列とポインタ

C


#include <stdio.h>

int main(int argc, char* argv[])
{
int i;
int vc[] = {10, 20, 30, 40, 50};
int* ptr = &vc[0];

for (i = 0; i < 5; i++)
{
printf("vc[%d] = %d " , i, vc[i]);
printf("ptr[%d] = %d " , i, ptr[i]);
printf("*(ptr + %d) = %d\n", i, *(ptr + i));
}
puts("");

ptr = vc;
for (i = 0; i < 5; i++)
{
printf("vc[%d] = %d " , i, vc[i]);
printf("ptr[%d] = %d " , i, ptr[i]);
printf("*(ptr + %d) = %d\n", i, *(ptr + i));
}

return 0;
}

実行結果

T:\>lesson078\Project1.exe
vc[0] = 10 ptr[0] = 10 *(ptr + 0) = 10
vc[1] = 20 ptr[1] = 20 *(ptr + 1) = 20
vc[2] = 30 ptr[2] = 30 *(ptr + 2) = 30
vc[3] = 40 ptr[3] = 40 *(ptr + 3) = 40
vc[4] = 50 ptr[4] = 50 *(ptr + 4) = 50

vc[0] = 10 ptr[0] = 10 *(ptr + 0) = 10
vc[1] = 20 ptr[1] = 20 *(ptr + 1) = 20
vc[2] = 30 ptr[2] = 30 *(ptr + 2) = 30
vc[3] = 40 ptr[3] = 40 *(ptr + 3) = 40
vc[4] = 50 ptr[4] = 50 *(ptr + 4) = 50