I'm beginner in C language and currently playing with pointers to understand. The following code creates an array of ten elements and I try to output the address of the first and second array element by using pointers here:
#include <stdio.h>
int main()
{
int array[10];
int i;
for ( i = 0; i < 10; i++ ) {
array[ i ] = i;
}
int *ptr = &array[0];
int *ptr2 = &array[1];
printf("Element[%d] = %d at Address = %x\n", 0, *ptr, ptr);
printf("Element[%d] = %d at Address = %x\n", 1, *ptr2, ptr2);
return 0;
}
After compiling, I get the following output:
Element[0] = 0 at Address = 195ab640
Element[1] = 1 at Address = 195ab644
So the address of the first array element array[0]
and the second array element array[1]
are 195ab640
and 195ab644
. There is always 4 difference between consecutive array element address numbers.
I was expecting if the first array element's address is m the next element address would be m+1 but I come across m+4
. Why is the address is increased by four but not one?
I use this online compiler: https://www.onlinegdb.com/online_c_compiler