C Array Memory Storage and Address Calculation
C
Easy
3 views
Problem Description
When we declare an array in C language, such as int numbers[10],
how exactly is it stored in memory, and how does each element get its memory address?
Official Solution
#include <stdio.h>
int main() {
int numbers[10];
int i;
// Value fill kar dete hai
for (i = 0; i < 10; i++) {
numbers[i] = (i + 1) * 10; // 10,20,30...
}
printf("Array ka base address (numbers): %pn", (void*)numbers);
printf("First element ka address (&numbers[0]): %pnn", (void*)&numbers[0]);
printf("Index Value Address Formula Checkn");
printf("--------------------------------------------------------n");
for (i = 0; i < 10; i++) {
printf("%5d %5d %p base + %d * %lun",
i,
numbers[i],
(void*)&numbers[i],
i,
(unsigned long)sizeof(int));
}
printf("nPointer notation test:n");
printf("*(numbers + 3) = %dn", *(numbers + 3));
printf("Address of (numbers + 3) = %pn", (void*)(numbers + 3));
printf("Address of numbers[3] = %pn", (void*)&numbers[3]);
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!