Rakesh Beeram’s Post

Are arr and &arr the same thing in C? 🤔 int arr[10]; They both point to the same physical starting memory address! But they are fundamentally different from the compiler. Let’s look at the math if our array starts at address 1000: 📍 arr (Pointer to the 1st element) ->Evaluates to address 1000 ->Adding 1 moves forward by 1 integer (4 bytes) ->arr + 1 = 1004 📦 &arr (Pointer to the entire array) ->Evaluates to address 1000 ->Adding 1 moves forward by the whole array size (40 bytes) ->&arr + 1 = 1040 (the slot just after the array) 💥 The "No Size of" Length Trick: Size of Array without using the sizeof() function. Because &arr + 1 points to the address just after the array (1040), subtracting the starting address (1000) from it yields the exact number of elements! int length = *(&arr + 1) - arr; // gives the result of 10 ⚠️ Note: Dereferencing a one-past-the-end pointer is undefined behavior in standard C — this trick is purely for understanding pointer arithmetic, not for production code. Why dereferencing *(&arr + 1) : *(&arr + 1) dereferences the array pointer, and the result naturally decays from int[10] to int * — so the subtraction gives you element count, not bytes. When you subtract two pointers in C, the compiler looks at their data type and says, "These are pointers to integers. The programmer wants to know how many integers fit between these two addresses, not the raw number of bytes." It automatically applies the scale division without requiring you to use the sizeof() operator in your code. Summary: They share the same address, but they have completely different "step sizes" in pointer arithmetic. #CProgramming #Pointers #CodingTips  #SoftwareDevelopment #EmbeddedC #EmbeddedSystems

To view or add a comment, sign in

Explore content categories