1

Scope: Make a function that takes a 2d array as input and prints each element individually.

I start with just having the array within the function and this works perfectly

#include<iostream>
void printArray();
int main()
{
  printArray();
  return 0; 
}

void printArray()
{
  int array[4][2] = {{1,2},{3,4},{5,7},{8,9}};
  int rows = sizeof(array)/sizeof(array[0]);
 int cols = sizeof array[0] / sizeof array[0][0];
 for (int i = 0 ; i < rows ; i++)
   {
     for ( int j = 0; j < cols; j++ )
       {
     std::cout<<array[i][j]<<' ';
       }
     std::cout<<'\n';
   }

 std::cout<<rows<<"    "<<cols;

}

So I move on and tried to make printArray() take an input

#include<iostream>
void printArray(int array);
int main()
{
  int arr[4][2] = {{1,2},{3,4},{5,7},{8,9}};
  printArray(arr);
  return 0; 
}

void printArray(int array)
{

 int rows = sizeof(array)/sizeof(array[0]);
 int cols = sizeof array[0] / sizeof array[0][0];
 for (int i = 0 ; i < rows ; i++)
   {
     for ( int j = 0; j < cols; j++ )
       {
     std::cout<<array[i][j]<<' ';
       }
     std::cout<<'\n';
   }

 std::cout<<rows<<"    "<<cols;

}

now I am getting an error call: error: invalid types ‘int[int]’ for array subscript

what is going wrong ?

the output I want is

1 2 
3 4 
5 7 
8 9 
4    2
1
  • 1
    Your function asks for an int type as opposed to an int array type Commented Feb 28, 2020 at 3:20

2 Answers 2

1

Just modified you code and commented where changes were required:

#include<iostream>

// as you are passing an array to your function,
// this is one valid syntax to do so:
void printArray(int array[4][2], int rows, int cols);

int main()
{
  int arr[4][2] = {{1,2},{3,4},{5,7},{8,9}};

  // You can't get sizeof arrays that have been passed to
  // a function. Functions accept them as pointers
  // and sizeof(arr) inside the function will actually
  // return the sizeof pointer.
  int rows = sizeof(arr)/sizeof(arr[0]);
  int cols = sizeof(arr[0])/ sizeof(arr[0][0]);

  printArray(arr, rows, cols);
  return 0;
}

void printArray(int array[4][2], int rows, int cols)
{

  for (int i = 0 ; i < rows ; i++)
   {
     for ( int j = 0; j < cols; j++ )
       {
     std::cout<<array[i][j]<<' ';
       }
     std::cout<<'\n';
   }

 std::cout<<rows<<"    "<<cols;

}



Some reading material:

How are arrays passed to functions.

sizeof operator on an array passed to a function

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your comment it was very helpful, in addition to the reading material you provided i also found a youtube video explaining this which I found helpfull Tutorial 47 - Passing Arrays to Functions.
1

You're passing an integer and not an array.

There are 3 ways through which you can pass a 2D array as parameter

Method1: Pass it as a pointer which holds ints

int *arr[4];
for (i = 0; i < 2; i++)
    arr[i] = new int[3]
void printArray(int *arr[4]) { ... }

Method2: Pointer of pointers

int **arr;
arr = new int *[4];
for (i = 0; i < 2; i++)
    arr[i] = new int[3];
void printArray(int **arr) { ... }

Method3:

Here your parameter itself is a 2D array

int arr[4][2] = {{1,2},{3,4},{5,7},{8,9}};
void printArray(int arr[][2]) { ... }

Comments

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.