given
double ar[20];
and a function call
function(ar,size);
is this equivalent to
function(&ar[0],size)
?????
whats the difference between
function(ar+1,size)
and
function(&ar[1],size)
????
given
double ar[20];
and a function call
function(ar,size);
is this equivalent to
function(&ar[0],size)
?????
whats the difference between
function(ar+1,size)
and
function(&ar[1],size)
????
Presumably, your function declaration would look like this:
void function(double* ar, int size);
In this case, the calls:
function(ar, 20);
and
function (&ar[0], 20);
are equivelent. In both cases you are passing a pointer to the first element in an array of 20 doubles. In the second case, you have de-referenced the first element to a double, and then re-referenced it to a pointer with the & character.
You can use this technique to pass a pointer to the second character (as your example):
function(&ar[1], 19);
Notice I have decremented the size value, as there are only 19 elements in the array pointed to by &ar[1].
The 'function(ar+1,size)' looks incorrect. Remember ar is an 'address' of an array of doubles, not a double itself.
The 'function(ar+1,size)' looks incorrect. Remember ar is an 'address' of an array of doubles, not a double itself.
function(ar+1, size-1);
is the same as
function(&ar[1], 19);
the 'ar+1' increments the address stored in ar by the size of 1 double.
OK, I take it back. &ar[1] & ar + 1 are equivalent in that case. Never used that notation before.
Just a note. Things likeor maybeCode:ar+1are called pointer arithmetics. It's very useful when using e.g. linked lists. When travelling through it, you don't need to declare additional integer variables.Code:++ar
Please excuse my poor english...
Bravo, Larry! Thank you for pointing that out. Pointer arithmetic is very important in programming C/C++.