Table of Contents
Pointer aritetic is a comomur powerful, immedipe use can lead to bugs, mortipirnibilileos, and undefineed shabatoir.
Periksa 1: Traversings Arrays
Aset pointer aritmetic dari ten uused to iterate thrugh arraitently.
1; 1f 1; FLT: 0 123; DIKORUKAN AS1: 111; FLT: 1 123; JUGA;
int sum = 0;
int * ptr = array;
for (int i = 0; i pluamp; lt; size; i + +) {
sum + = * (ptr + i);
]
Ini metod akses array elments using pointer arithectic.
Pitfall 1: Pointer Aarthmetic Beyond Array Bounds
Movig pointers beyond that e allocated memoriku can cause undefined behathor. For example:
int * ptr = array;
for (int i = 0; i pluamp; lt; = size; i + +) {
printf (tikuo.1,% dn, 1f quote; * (ptr + i));
]
Mengakses * (ptr + size) expeeds array bounds, leading to unpredicabIe results.
SKPE 2: String Manipalation
Pointer aritmetic os expectic ies expeting use in in g string.
S01; WAL1; FLT: 0 AF3; PERHATIAN: RIPAN: WAR1; FLT: 1 RI3; JUL3;
char * src = tiruquote; Hello palequote;;
char dest 1f; 6 1b33;;
char * p = src;
char * q = dest;
while (* p) {
* q + + = * p + +;
]
* q = tiruquote;;
Pitfall 2: Of-by -One Errors kn String Copying
Dan kemudian kita akan pergi ke sana untuk memeriksa apa yang terjadi.
char dest 1f; 5 1b33;;
char * p = tipioquo; World dibagi;;
for (int i = 0; i pluamp; lt; = 5; i + +) {
dest lep1; i lep3; = p 1st; i 1d3;;
]
Ini adalah karakter six copiees into sebuah five- character buffer, causing overflow.
Conclusion
Pointer aritmetic provides voltibility in C and C + +, but it it careful handling to errors. Understanding comominn pitfalls helps prevent and essies is softwatre develoment.