1

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?

2 Answers 2

3

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! Would you mind kind of explaining this? I thought arrays are already basically pointers to their first value. And what does int** in the function do?
Sure. int** is just syntactic sugar for int *arr[], It's commonly used for passing arrays to functions because as you said you sometimes could be interpreted as a pointer to the first value. int* arr[] declares an array of pointers to integers where the square brackets set the length of the array. The pointer means what this array accepts can be an array of integers (because it can be viewed as a pointer) or even just a pointer to a simple number.
I appreciate it! I think I get it.
1

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 index into the array arr by using the expression arr[3][5] inside the body of the function thisFunc, then, in order to retrieve the corresponding value from the array, 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

6 Comments

Thanks for the answer! Is there an advantage to using this code as opposed to pointers as the other answer shows? This one requires a memcpy for each inner array, which isn't the worst thing, but if I needed to do lots and lots of inner arrays, it might get clunky.
@Dima: I have now changed my solution to not use memcpy and have added an additional explanation. Is that more like what you want?
No, because what I have is a long list of little arrays (bob[], jim[], sally[] etc), and I want to easily swap custom arrangements of little arrays into arr[]. For example, I'd like to easily be able to make an array that consists of {bob[], bob[], jim[], sally[], betsy[]}, so that all I need to know to construct the array is the names, instead of the integer values.
@Dima: In your question, you stated: "I'd like to pass an array [...] which contains multiple arrays itself". My solution does exactly what you asked for in your question. The other answer's solution does not pass an array which contains arrays, but rather passes an array which contains pointers (i.e. references) to arrays. Therefore, the other solution does not do exactly what you asked for in the question (but something similar). Which solution is better depends on the situation.
@Dima: Based on the information in your previous comment, the other answer is probably better for what you want to do.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.