指针算术用于以C语言实现算术运算,例如加减法,增量等。有四种指针算法,例如加法,减法,增量和减量。在32位计算机中,它将值增加或减少2,并将增加或减少2 *数字。在64位计算机中,它将值增加或减少4,并将增加或减少4 *数字。
这是C语言中的指针算术示例,
#include<stdio.h> int main() { int val = 28; int *pt; pt = &val; printf("Address of pointer : %u\n",pt); pt = pt + 5; printf("Addition to pointer : %u\n",pt); pt = pt - 5; printf("Subtraction from pointer : %u\n",pt); pt = pt + 1; printf("Increment to pointer : %u\n",pt); pt = pt - 1; printf("Decrement to pointer : %u\n",pt); return 0; }
输出结果
Address of pointer : 3938439860 Addition to pointer : 3938439880 Subtraction from pointer : 3938439860 Increment to pointer : 3938439864 Decrement to pointer : 3938439860
在上述程序中,算术运算(加法,减法等)应用于指针变量* pt。
int *pt; pt = &val; printf("Address of pointer : %u\n",pt); pt = pt + 5; printf("Addition to pointer : %u\n",pt); pt = pt - 5; printf("Subtraction from pointer : %u\n",pt); pt = pt + 1; printf("Increment to pointer : %u\n",pt); pt = pt - 1; printf("Decrement to pointer : %u\n",pt);