# 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
}
``````

``````Address of p variable is 11926728
``````

## 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
``````