Cにおけるポインタ(インクリメント、減算、加算、減算)
1289 ワード
#include <stdio.h>
int main (void)
{
int urn[5] = {100,200,300,400,500};
int *ptr1,*ptr2,*ptr3;
ptr1 = urn;//
ptr2 = &urn[2];//
//
//
printf("pointer value,dereferenced pointer,pointer address:
");
printf("ptr1 = %p,*ptr1 = %d,&ptr1 = %p
",ptr1,*ptr1,&ptr1);
//
ptr3 = ptr1 + 4;
printf("
adding an int to a pointer:
");
printf("ptrl + 4 = %p, *(ptr1 + 3) = %d
",ptr1 + 4,*(ptr1+3));
ptr1++;//
printf("
values after ptrl++
");
printf("ptrl = %p, *ptrl = %d, &ptrl1 = %p
",ptr1,*ptr1,&ptr1);
ptr2--;//
printf("
values after --ptr2
");
printf("ptr2 = %p,*ptr2 = %d,&ptr2 = %p
",ptr2,*ptr2,&ptr2);
--ptr1;//
++ptr2;//
//
printf("
Pointers reset to original values:
");
printf("ptr2 = %p,ptrl = %p,ptr2-ptr1 = %d
",ptr2,ptr1,ptr2-ptr1);
//
printf("
subtracting an int from a pointer:
");
printf("ptr3 = %p,potr3 - 2 = %p
",ptr3,ptr3 - 2);
return 0;
}