Passing array in a function

Posted on December 9, 2019

An array in c is like a pointer to data and passes in a function easy

int myarray[10];
void myfunction(int[10]);

int main() {
  myfunction(myarray);
}

void function myfunction(int parray[10]) {
  ...
}

With sizeof() you can get the size of the array, so no need to define the dimensions

int myarray[10];
void myfunction(int[]);

int main() {
  myfunction(myarray);
}

void function myfunction(int parray[]) {
  int n = sizeof(parray);
  ...
}

In case of multidimensional array you can let the most only one dimension undefened, so the compiler can calculate the size of the array

int myarray[10];
void myfunction(int[][4]);

int main() {
  myfunction(myarray);
}

void function myfunction(int parray[][4]) {
  int n = sizeof(parray)/sizeof(parray[0]);
  ...
}