Pass an array to function using pointer in C? -


first declare array a 10 elements. call function bubblesort bubblesort( a, 10); bubblesort function declared void bubblesort(int* const array, const int size)

my question if "array" pointer- means stored address of array a (array= &a [0]) how can understand these terms array[1], array[2], array[3]... in function bubblesort?

it bubble sort program , part confusing me.

array[1] means, definition in c standard, *(array+1). so, if array pointer, expression adds 1 element pointer, uses result access pointed-to object.

when a array, may used thinking of a[0], a[1], a[2], , on elements of array. go through same process pointer above, 1 step. when compiler sees a[1] , a array, compiler first converts array pointer first element. rule in c standard. a[1] (&a[0])[1]. definition above applies: (&a[0])[1] *(&a[0] + 1), means “take address of a[0], add 1 element, , access object result points to.”

thus, a[1] in calling code , array[1] in called code have same result, though 1 starts array , other uses pointer. both use address of first element of array, add 1 element, , access object @ resulting address.


Comments