Pointer Arithmetic & Const Correctness
পয়েন্টার arithmetic ও const correctness
1. Pointer Arithmetic Is Type-Sized
p + 1 doesn't add 1 byte — it adds sizeof(*p) bytes. So an
int* increments by 4, a double* by 8.
p + 1-এর অর্থ হলো p + sizeof(*p) বাইট এগিয়ে যাওয়া। অর্থাৎ pointer-এর ধরন অনুযায়ী এক ধাপ-এর আকার নির্ধারিত হয়।
#include <iostream>
int main() {
int a[] = {10, 20, 30, 40};
int* p = a;
std::cout << *p << "\n"; // 10
std::cout << *(p+1) << "\n"; // 20
std::cout << p[2] << "\n"; // 30 (p[i] == *(p+i))
std::cout << (p+1) - p << "\n"; // 1 (not 4!)
}
2. Arrays Decay to Pointers
When you pass an array to a function, it silently becomes a pointer to its first element.
Size info is lost. This is one major reason to prefer std::vector or std::array.
#include <iostream>
void f(int arr[]) {
std::cout << sizeof(arr) << "\n"; // 8 (a pointer!), not 20
}
int main() {
int a[5] = {1,2,3,4,5};
std::cout << sizeof(a) << "\n"; // 20
f(a);
}
3. const Placement — Three Variants
| Declaration | Meaning |
|---|---|
const int* p | Pointer to const int — cannot modify *p, but can change p. |
int const* p | Same as above (const binds to int). |
int* const p | Const pointer — cannot change p, but can modify *p. |
const int* const p | Both fixed. Cannot change p nor *p. |
const int* p = "p is a pointer to int that is const". int* const p = "p is a const pointer to int".
4. Const Correctness
Mark anything that doesn't change as const. The compiler enforces it; readers benefit.
#include <iostream>
#include <string>
// "I won't modify s. The reader knows."
size_t countA(const std::string& s) {
size_t c = 0;
for (char ch : s) if (ch == 'a') ++c;
return c;
}
int main() {
std::string s = "banana";
std::cout << countA(s) << "\n";
}
5. Reading Complex Declarations
Read right-to-left, and group by parens:
| Declaration | Meaning |
|---|---|
int* p[10] | Array of 10 pointers to int |
int (*p)[10] | Pointer to an array of 10 ints |
int* f() | Function returning int* |
int (*f)() | Pointer to function returning int |
6. Practice Problems
- Given
int a[]={1,2,3,4,5}; int* p=a;, what is*(p+3)?✨ Show Answer
4 (a[3]).
- Are
p[2]and*(p+2)equivalent?✨ Show Answer
Yes, by definition.
- If
pisdouble*, by how many bytes doesp+1advance?✨ Show Answer
8 bytes (sizeof double).
- Read:
const int* p✨ Show Answer
Pointer to a const int. Can change which int
ppoints to, but can't modify the int throughp. - Read:
int* const p✨ Show Answer
Const pointer to int. The pointer itself can't be changed, but the int can.
- Why does sizeof differ inside a function vs outside?
✨ Show Answer
Arrays decay to pointers when passed. Inside the function, you have a pointer; sizeof gives pointer size (8 on 64-bit), not array size.
- Use a pointer to iterate an array of 5 ints.
✨ Show Answer
int a[] = {1,2,3,4,5}; for (int* p = a; p < a + 5; ++p) std::cout << *p << " "; - Write a function with signature
void print(const int* arr, size_t n).✨ Show Answer
void print(const int* arr, size_t n) { for (size_t i = 0; i < n; ++i) std::cout << arr[i] << " "; } - What's
const char*good for?✨ Show Answer
String literals:
const char* msg = "hello";. The literal lives in read-only memory; modifying it is UB. - Why must member functions be marked
constwhen they don't modify state?✨ Show Answer
So you can call them on
constobjects. Without the const-qualifier, calling on a const object fails to compile. - Difference between
const int* pandint const* p?✨ Show Answer
None. Both: pointer to const int.
- Can you have a
const int* const p?✨ Show Answer
Yes. Both target and pointer are const. Useful for "fixed pointer to fixed data".
- Show that const-cast can strip const (and is dangerous).
✨ Show Answer
const int x = 5; int* p = const_cast<int*>(&x); *p = 10; // UB if x was originally constDon't do this except for legacy C-API interop.
- What's the result of
p2 - p1for two pointers into the same array?✨ Show Answer
The number of elements between them, not bytes. Type:
std::ptrdiff_t. - Read:
void (*pf)(int)✨ Show Answer
Pointer to a function taking int and returning void.
- Why is iterating with
begin()/end()better than raw pointers?✨ Show Answer
Iterators work for any container (vector, list, map, ...). Raw pointer iteration only works for arrays/vectors. Iterators integrate with STL algorithms.
- Can you increment a
const int*?✨ Show Answer
Yes. The pointer itself isn't const, only what it points to.
p++is allowed. - Write a const-correct function that returns the length of a C-string.
✨ Show Answer
size_t strlen_my(const char* s) { size_t n = 0; while (*s++) ++n; return n; }
Summary
Pointer arithmetic moves in type-sized steps. Arrays decay to pointers when passed — losing size info.
Read declarations right-to-left to decode const: const T* = pointer to const,
T* const = const pointer. Be aggressively const-correct; the compiler will help you.