Skip to main content
fixed addition error
Source Link
brianxautumn
  • 1.2k
  • 9
  • 21

This is what is happening: An array is just a contiguous chunk of memory.

&test 

Is getting the address of that index of the starting point of array. Not the value.

When you add [some number], it counts up the number times the size of the data type, in this case each char is a byte.

So when you do

&test[i] 

that means the starting address + i bytes.

when you do

(&test[i])[i] 

You are doing i bytes from the starting address, and then treat that as the starting address and go up i more bytes.

So in your iterations:

(&test[0])[0] // index 0 + 0 = 0 (&test[1])[1] // index 1 + 1 = 12 (&test[2])[2] // index 2 + 2 = 4 (&test[3])[3] // index 3 + 3 = 6 

This is what is happening: An array is just a contiguous chunk of memory.

&test 

Is getting the address of that index of the starting point of array. Not the value.

When you add [some number], it counts up the number times the size of the data type, in this case each char is a byte.

So when you do

&test[i] 

that means the starting address + i bytes.

when you do

(&test[i])[i] 

You are doing i bytes from the starting address, and then treat that as the starting address and go up i more bytes.

So in your iterations:

(&test[0])[0] // index 0 + 0 = 0 (&test[1])[1] // index 1 + 1 = 1 (&test[2])[2] // index 2 + 2 = 4 (&test[3])[3] // index 3 + 3 = 6 

This is what is happening: An array is just a contiguous chunk of memory.

&test 

Is getting the address of that index of the starting point of array. Not the value.

When you add [some number], it counts up the number times the size of the data type, in this case each char is a byte.

So when you do

&test[i] 

that means the starting address + i bytes.

when you do

(&test[i])[i] 

You are doing i bytes from the starting address, and then treat that as the starting address and go up i more bytes.

So in your iterations:

(&test[0])[0] // index 0 + 0 = 0 (&test[1])[1] // index 1 + 1 = 2 (&test[2])[2] // index 2 + 2 = 4 (&test[3])[3] // index 3 + 3 = 6 
Source Link
brianxautumn
  • 1.2k
  • 9
  • 21

This is what is happening: An array is just a contiguous chunk of memory.

&test 

Is getting the address of that index of the starting point of array. Not the value.

When you add [some number], it counts up the number times the size of the data type, in this case each char is a byte.

So when you do

&test[i] 

that means the starting address + i bytes.

when you do

(&test[i])[i] 

You are doing i bytes from the starting address, and then treat that as the starting address and go up i more bytes.

So in your iterations:

(&test[0])[0] // index 0 + 0 = 0 (&test[1])[1] // index 1 + 1 = 1 (&test[2])[2] // index 2 + 2 = 4 (&test[3])[3] // index 3 + 3 = 6