Your function is expecting a pointer to the structure. This arguement can be an individual instance of that structure or it could be an element in the array of the give structure. Like
struct myStruct {
int a, b;
long cL, dL;
char e;
} struc1, struc2, record[20];
and function's prototype will be
function( struct myStruct *ptr);
Now you can pass the structure to function:
function( &struct1 );
// or
function( &record[ index] );
Now your confusion arises because of the misconception that syntax array[i]
can also be treated as a pointer like we can do with the name of the array
.
record
- name of the array- gives the address of the first member of the array, (pointers also point to memory addresses) hence it can be be passed to the function. But record[index]
, it is different.
Actually, when we write record[ index]
it gives us the value placed there which is not a pointer. Hence your function which is accepting a pointer, does not accept it.
To make it acceptable to the function, you will have to pass the address of the elements of the array i.e
function( &record[ index ] );
Here &
operator gives the address of the elements of the array.
Alternatively, you can also use:
function( record + index );
Here, as we know record
is the address of the first element, and when we add index
in it, it gives the address of the respective element using pointer arithmetic.
Hope it was helpful.