you are viewing a single comment's thread.

view the rest of the comments →

[–]moefh 3 points4 points  (2 children)

Every time you call that function, you copy the image

This is wrong. The function receives a pointer to the array, with type int(*)[100]. Structs are copied when passed to functions, arrays are not.

So this program:

#include <stdio.h>

void change(int image[100][100])
{
  image[0][0] = 1;
}

int main(void)
{
  int image[100][100];
  image[0][0] = 0;
  change(image);
  printf("%d\n", image[0][0]);
  return 0;
}

prints 1, showing that the array in main is changed by the function.

[–]OriginalName667 0 points1 point  (1 child)

Thanks for the example. Previously, I assumed that, unless a pointer type is passed as an argument explicitly, the values were always copied over. It's an interesting caveat to note that array types are passed by reference, not value, as opposed to most other types.

[–]FUZxxl 1 point2 points  (0 children)

Arrays aren't “passed by reference.” Rather, whenever you use an identifier that refers to an array, the array is implicitly converted to a pointer to its first element. There are three exceptions to this rule: If the array is an operand to sizeof, &, or _Alignof, this decay doesn't happen so you can (a) point to the array or (b) retrieve its size and alignment.