# C語言指針算術運算

• 遞增
• 遞減
• 加法
• 減法
• 比較

## 1.遞增指針

``new_address= current_address + i * size_of(data type)``

32位

64位

``````#include <stdio.h>
void main() {
int number = 50;
int *p;//pointer to int
p = &number;//stores the address of number variable

printf("Address of p variable is %u \n", p);
p = p + 1;
printf("After increment: Address of p variable is %u \n", p);
}``````

``````Address of p variable is 17824724
After increment: Address of p variable is 17824728``````

## 2.指針遞減

32位

64位

``````#include <stdio.h>
void main() {
int number = 50;
int *p;//pointer to int
p = &number;//stores the address of number variable

printf("Address of p variable is %u \n", p);
p = p - 1;
printf("After decrement: Address of p variable is %u \n", p);
}``````

``````Address of p variable is 18218432
After decrement: Address of p variable is 18218428``````

## 3.指針相加

``new_address= current_address + (number * size_of(data type))``

32位

64位

``````#include <stdio.h>
void main() {
int number = 50;
int *p;//pointer to int
p = &number;//stores the address of number variable

printf("Address of p variable is %u \n", p);
p = p + 3;   //adding 3 to pointer variable
printf("After adding 3: Address of p variable is %u \n", p);
}``````

``````Address of p variable is 11926728
After adding 3: Address of p variable is 11926740``````

## 4.指針減法

``new_address= current_address - (number * size_of(data type))``

32位

64位

``````#include <stdio.h>
void main() {
int number = 50;
int *p;//pointer to int
p = &number;//stores the address of number variable

printf("Address of p variable is %u \n", p);
p = p - 3; //subtracting 3 from pointer variable
printf("After subtracting 3: Address of p variable is %u \n", p);
}``````

``````Address of p variable is 17825120
After subtracting 3: Address of p variable is 17825108``````