How to Pass a Two-Dimensional Array to a Function.
In one of my articles written a few years ago, I described what pointers are used for in C and how to work with them. Rereading that article today, I realized there is another interesting question worth looking at: "How do you pass a two-dimensional array to a function?"
The answer suggests itself: use a pointer. But simply passing the address of the first element of a two-dimensional array (essentially a pointer) is not enough; let's figure out why.
Even though a two-dimensional array is essentially a table (an array whose elements are arrays), all its elements are stored sequentially in memory, and to navigate this sequence you need to know the size of its second dimension. So if we have an array arr[5][20], to work with it it is enough to know the address it starts at and that the size of its second dimension is 20.
Then the formula for calculating the address of any array element looks like this
arr + 20*i + j
arr — the start address of the array;
i — the first index of the element;
j — the second index of the element;
A two-dimensional array can be used to store strings describing some entity. In that case, a function that modifies it will look like this.
Similarly, you can pass a pointer while specifying the size of the array's second dimension
Or pass the array while specifying the size of its second dimension.
Which of the above methods to use is up to you; once you understand the essence, it doesn't matter much.
The answer suggests itself: use a pointer. But simply passing the address of the first element of a two-dimensional array (essentially a pointer) is not enough; let's figure out why.
Even though a two-dimensional array is essentially a table (an array whose elements are arrays), all its elements are stored sequentially in memory, and to navigate this sequence you need to know the size of its second dimension. So if we have an array arr[5][20], to work with it it is enough to know the address it starts at and that the size of its second dimension is 20.
Then the formula for calculating the address of any array element looks like this
arr + 20*i + j
arr — the start address of the array;
i — the first index of the element;
j — the second index of the element;
A two-dimensional array can be used to store strings describing some entity. In that case, a function that modifies it will look like this.
void Update_List_Name(char *arr, uint8_t j)
{
char a;
uint32_t adr;
//the first letter of the first string is at the start address of the array
adr = (arr+0*j+0);
//the second letter of the first string is located at address
adr = (arr+0*j+1) ;
//the second letter of the first string is located at address
adr = (arr+1*j+0) ;
//the second letter of the first string is located at address
adr = (arr+1*j+1)
// to get the data stored in a cell,
// you need to use the dereference operation
a = *adr;
}Similarly, you can pass a pointer while specifying the size of the array's second dimension
void Update_List_Name(char *arr[20])
{
char a;
//in this case the array elements can be accessed as usual
a = arr[1][2];
}Or pass the array while specifying the size of its second dimension.
void Update_List_Name(char arr[][20])
{
char a;
//in this case the array elements can be accessed as usual
a = arr[1][2];
}Which of the above methods to use is up to you; once you understand the essence, it doesn't matter much.