Greg Peckory Greg Peckory - 1 month ago 7
C Question

Why can function parameter contain addresses or variables in C

Looking up some tutorials online I noticed two different MPI_SEND uses:

The first says the start of the buffer is

&a[500]
:

MPI_Send(&a[500], 500, MPI_INT, 1, 0, MPI_COMM_WORLD);


The second says the start of the buffer is
b
:

MPI_Send(b, 500, MPI_INT, 0, 0, MPI_COMM_WORLD);


I'm very new to
C
so maybe it has something to do with pass by reference / value.

My guess is that
b
is the same as
&b
, but in the first case swe use
&a[500]
because we are starting with an address in the middle of an array??

But why can't you just say
a[500]
instead of
&a[500]
?

Answer

The term &a[500] is the address of a[500], that is the 500st element of the array a. So it is a pointer.

The term b can be either an array (e.g. int b[1000]) in which case, without index, it is converted to the pointer to the start of the array (i.e. &b[0] ), or a p real pointer, pointing to the start of some array.

Remarks: a[500] (without the ampersand) would be the value of the 500st element.