๐
Date: Jan 21, 2026
๐ฅ Topic: Relationship between Pointers & Arrays
๐คฏ The Big Reveal
Today I learned a shocking fact: The name of an array is actually a pointer to its first element.
If I have int arr[5], then just writing arr gives me the address of arr[0].
๐ป Day 21 Code: Accessing Array via Pointer
I can access array elements without using square brackets []!
#include <iostream>
using namespace std;
int main() {
int marks[] = {90, 80, 70};
// Pointing to the first block
int *p = marks;
cout << "First element (*p): " << *p << endl;
// Moving to next block logic (Internal)
cout << "Second element *(p+1): " << *(p+1) << endl;
// Formula: arr[i] == *(arr + i)
cout << "Third element *(arr+2): " << *(marks+2) << endl;
return 0;
}
๐ญ Thoughts
This explains why Arrays are 0-indexed. The index is just an "offset" from the starting address. *(arr + 0) is the start.
No comments:
Post a Comment