Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the addition mean in function(1,a+2) if a is an array?

Tags:

arrays

c

I was looking at this piece of code and I'm not sure what the addition means if "a" is an array.

int main(int argc, char* argv[]){
int a[] = {1, 3, 5, 7, 9};

function(1, a+2);
return 0;}

Assume the function is already created.

like image 660
Mark Avatar asked Feb 14 '23 23:02

Mark


1 Answers

In an expression (except when used with sizeof or &), an array name is a pointer to the first element in the array. So a+2 is "pointer arithmetic" on that pointer, and results in a pointer to the element at offset 2. It is equivalent to &a[2].

like image 166
ooga Avatar answered Mar 08 '23 03:03

ooga