Here’s the code that shows the basic idea of what I’m trying to do:
#include <stdio.h>
void thisFunc(int arr){
int firstValofBob = arr[0][0];
int secondValofBob = arr[0][1];
}
int main()
{
int bob[2] = {12, 13};
int jim[2] = {20, 50};
int arr[2] = {bob, jim};
thisFunc(arr);
}
I’d like to pass an array (arr[]) which contains multiple arrays itself (bob[] and jim[]) to a function, so that I can access the values inside bob[] and jim[].
I know the code here will not work, and that I probably need to use pointers in some way. Suggestions for a good way to do this?
>Solution :
To store the values of both bob and jim, you need to create an array that stores array of integers and then pass it to the function. My implementation is:
#include <stdio.h>
void thisFunc(int** arr){
int firstValofBob = arr[0][0];
int secondValofBob = arr[0][1];
}
int main()
{
int bob[2] = {12, 13};
int jim[2] = {20, 50};
int* arr[2] = {bob, jim};
thisFunc(arr);
}