I have a question about using pass-by-reference for 2D arrays (VLA variants) in C. It seems most of the examples demonstrated, like #2 here: How to pass 2D array (matrix) in a function in C? shows that you don't have to use pass-by-reference convention. Just to show my example:
#include <stdio.h>
#include <stdlib.h>
void assign(double** testMatrix, int* dim){
for(int row=0; row < *dim; ++row){
for(int column=0; column< *dim; ++column){
testMatrix[row][column] = 0;
}
}
}
int main(void) {
int dim = 200;
double** testMatrix = malloc(sizeof(double*) * dim);
for(int i=0; i < dim; ++i){
testMatrix[i] = malloc(sizeof(double) * dim);
}
assign(testMatrix, &dim);
//deallocate test matrix
for(int i=0; i< dim; ++i){
free(testMatrix[i]);
}
free(testMatrix);
return 0;
}
the above sample code assigning the 2D array without using conventions for pass-by-reference, like the sample below (see assign function with the &):
#include <stdio.h>
#include <stdlib.h>
void assign(double*** testMatrix, int* dim){
for(int row=0; row < *dim; ++row){
for(int column=0; column< *dim; ++column){
(*testMatrix)[row][column] = 0;
}
}
}
int main(void) {
int dim = 200;
double** testMatrix = malloc(sizeof(double*) * dim);
for(int i=0; i < dim; ++i){
testMatrix[i] = malloc(sizeof(double) * dim);
}
assign(&testMatrix, &dim);
//deallocate test matrix
for(int i=0; i< dim; ++i){
free(testMatrix[i]);
}
free(testMatrix);
return 0;
}
My question is how is the first example's 2D array modified without passing the reference of the array?