Home > database >  How can I pass an array inside an array to a function in C?
How can I pass an array inside an array to a function in C?

Time:05-31

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?

CodePudding user response:

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);
}

CodePudding user response:

When passing a multidimensional array to a function, the function must know the size of all dimensions of the array, except for the outermost dimension. Otherwise, if these sizes are unknown, then the compiler will not know how to calculate the memory address of the array elements.

For example, if you write arr[3][5] in the function thisFunc, then the compiler doesn't need to know the size of the outer array, but it needs to know the size of the inner array. If it for example knows that the size of the innermost array is 8, then it will know that you want to access the 30th element of the array (3*8 5==29, which corresponds to index 29 in 0-based indexing and index 30 in 1-based indexing).

In this case, you seem to want the size of the inner arrays to be 2, so you should change the line

void thisFunc(int arr){

to:

void thisFunc(int arr[][2]){

Another issue is that the line

int arr[2] = {bob, jim};

will not work, for two reasons:

  1. The type of the array should be int arr[2][2] instead of int arr[2] if you want it to contain two sub-arrays of two elements each.

  2. You cannot copy an array like that. However, you can copy an array using the function memcpy.

Here is an example of copying an array using memcpy:

int bob[2] = {12, 13};
int jim[2] = {20, 50};

int arr[2][2];

memcpy( arr[0], bob, sizeof arr[0] );
memcpy( arr[1], jim, sizeof arr[1] );

You can also initialize the array content directly, for example like this:

int arr[2][2] = {
    { 12, 13 }, //bob
    { 20, 50 }  //jim
};

That way, you won't need the additional arrays bob and jim and you also won't need to use memcpy.

The entire code would look like this, assuming that the inner array has a fixed size of 2 elements:

#include <stdio.h>
#include <string.h>

void thisFunc( int arr[][2] )
{
    int firstValOfBob = arr[0][0];
    int secondValOfBob = arr[0][1];

    printf( "firstValOfBob: %d\n", firstValOfBob );
    printf( "secondValOfBob: %d\n", secondValOfBob );
}
 
int main()
{
    int arr[2][2] = {
        { 12, 13 }, //bob
        { 20, 50 }  //jim
    };

    thisFunc(arr);
}

This program has the following output:

firstValOfBob: 12
secondValOfBob: 13
  •  Tags:  
  • c
  • Related