1

I'd like to make a function that receives a 2-dimensional array and returns one of its rows ('which') as a simple array. I wrote this:

int *row(int *array, int lines, int columns, int which)
{
    int result[columns];

    for (int i=0; i<columns; i++)
    {
        result[i] = *array[which][i];
    }
    return result;
}

However, in line 7 I got the following error: invalid types 'int[int]' for array subscript. Any idea how to do this properly? I also tried to handle the 2D-array as an array of arrays, but didn't succeed. I'm novice, so please avoid too advanced concepts.

Thanks for the help!

UPDATE: thanks for the help! Now my code looks like:

int n;  //rows
int m;  //columns
int data[100][100];   
int array[100];

int *row(int *array, int rows, int columns, int which)
{
    int* result = new int[columns];
    for (int i=0; i<columns; i++)
    {
        result[i] = *array[which*columns+i];
    }
    return result;
    delete[] result;
}

int main()
{
    array=row(data, n, m, 0);
}

I still get an error in main: incompatible types in assignment of 'int*' to 'int [100]'

What could be the problem now? I also don't know where to use the delete[] function to free up the array.

Thank you very much for the help!

1
  • To pass in a 2-dimensional array I think the array parameter should be an 'int **', otherwise you're passing in a 1-dimensional array. Commented Apr 11, 2012 at 14:29

6 Answers 6

4

You can't just do this:

int result[columns];

You need a dynamic allocation:

int* result = new int[columns];

Also, your use of array looks wrong. If array is going to be a single pointer, then you want:

result[i] = array[which*columns + i];
Sign up to request clarification or add additional context in comments.

Comments

2

"array" is one dimensional. You can access the element with index [which][i] via: array[which*columns + i]. Also remove the asterisk as the array is only a single pointer.

EDIT: Also you can not return local array - you need to deal with dynamic memory:

int* result = new int[columns];

And then take special care to free this memory. Other option would be to use std::vector.

1 Comment

s/remove local array/return local array/ ?
1

There are few errors that needs to be fixed first.

  1. You should never return a pointer to a local variable from a function. In the above code, you are trying to return a pointer to contents of 'result' which is a local variable.
  2. An array cannot be declared with a size that is variable, in your case the variable columns.
  3. If array is a two dimensional array, which i think is your intent, then array[which][i] gives you an int.You do not have to de-reference it.

Though I know I am not following the posting etiquette here, I recommend you to please start with a nice text book, grab the basics and come here when you encounter problems.

Comments

1

The size of an array needs to be a compile-time constant.

Instead of messing with arrays, you should probably use std::vector (possibly along with a 2D matrix class).

Comments

0

You can avoid all of this pointer arithmetic and memory allocation by using std::vector

#include <vector>
#include <iostream>

typedef std::vector<int> Row;
typedef std::vector<Row> Matrix;

std::ostream& operator<<(std::ostream& os, const Row& row) {
  os << "{ ";
  for(auto& item : row) {
    os << item << ", ";
  }
  return os << "}";
}

Row getrow(Matrix m, int n) {
  return m[n];
}

Row getcol(Matrix m, int n) {
  Row result;
  result.reserve(m.size());
  for(auto& item : m) {
    result.push_back(item[n]);
  }
  return result;
}

int main () {
  Matrix m = {
    { 1, 3, 5, 7, 9 },
    { 2, 4, 5, 6, 10 },
    { 1, 4, 9, 16, 25 },
  };

  std::cout << "Row 1: " << getrow(m, 1) << "\n";
  std::cout << "Col 3: " << getcol(m, 3) << "\n";  
}

Comments

0
double *row(double **arr, int rows, int columns, int which)
{
double* result = new double[columns];
for (int i=0; i<columns; i++)
{
    result[i] = arr[which][i];

}
return result;
delete[] result; 
}

This will return the row.

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.