two things that cause me problems :
- The first is that in the RAM each of the boxes that make up the table follows one another
This is true, supposing that by "table" you mean "array".
- The second is that a table is a pointer that points to the first box of the table
This is false, and manifestly incompatible with the previous statement.
However, in most places where an array's name or some other array-valued expression is evaluated, the (array) result is automatically converted to a pointer to the first element of the array.
when I do this I notice that the addresses do not follow each other :
int arrays[3] = {10, 20, 30};
int i = 0;
for (i=0 ; i < 3 ; i++)
{
printf("%d : %d\n", i, &arrays[i]);
}
Result : 0 : 973077712 1 : 973077716 2 : 973077720
Formally, that tells you nothing, because its behavior is undefined. The correct printf
formatting directive for pointers is %p
, not %d
, and technically, it is appropriate only for pointers to void
. The mismatch between the format directive and the data type of the corresponding argument produces UB. Instead, you want this:
printf("%d : %p\n", i, (void *) &arrays[i]);
However, you will likely find that the (hexadecimal) results still are not sequential numbers. That would be because objects of type int
require more than one unit of storage each. The difference between the addresses of consecutive int
units in your array is the size in bytes of one int
.
And when I do this I notice that array and &array give me the same
value which should not be the case for a "classic pointer" :
printf("value of arrays : %d\n", arrays);
printf("value of &arrays : %d\n", &arrays);
Result : value of arrays : 1522530048 value of &arrays : 1522530048
Again, undefined behavior. But you would see similar from the correct code. This is because of the automatic conversion of arrays to pointers that I described above, and from the fact that one of the few exceptions to that behavior is when an array-valued expression is the operand of the address-of (unary &
) operator. Thus arrays
is automatically converted to a pointer to the first array element, and &arrays
evaluates to a pointer to the whole array. These have different (pointer) types, but the address of the first element is the same as the address of the whole array.