arrays - A strange result in my C code -



arrays - A strange result in my C code -

i have next code in c:

int arr[] = {1,7,4,2,5,8}; int x = (&(arr[arr[1] - arr[4]]) - arr);

when run code, x = 2.

but if run this:

int arr[] = {1,7,4,2,5,8}; int = &(arr[arr[1] - arr[4]]); int b = arr; int x = a-b;

them x = 8.

why different values?

in case 8 equal 2 * sizeof( int )

in first code snippet there used pointer arithmetic while in sec code snipeet there used ordinary arithmetic integer numbers.

in expression

&(arr[arr[1] - arr[4]]) - arr

you deal pointers. between these 2 addresses ( &(arr[arr[1] - arr[4]]) , arr ) there two elements of array , first code snippet shows how many elements between these addresses. size of memory occupy equal `8 , sec code snippet shows that.

consider simple illustration more clear

int a[2];

sizeof( ) equal 8 2 * sizeof( int ). while sizeof( ) / sizeof(int ) equal 2. same value of expression

( + 2 ) -

c arrays

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -