954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Returning Array from function

Hello, basically I want to return an array from a function (So I don't output things in the class - just main)

But I seem to get the error: "invalid types 'int[int]' for array subscript"

Here is the code:

#include <iostream>

using namespace std;

int matrix()
{
	int matrix[10] = 
	{
		1, 1, 1, 1, 1,	
		1, 1, 1, 1, 1	
	};
	
	return *matrix;
	
}
int main(int argc, char *argv[]) {
	
	int matr = matrix();	

	for(int i=0; (i < 10); i++)
	{
		cout << matr[i] << "\t";		
	}
}


Any help would be great :)

phorce
Posting Whiz
362 posts since Jul 2011
Reputation Points: 31
Solved Threads: 26
 

If you attempt to return "matrix" without the *, what error does the compiler give?
Adjust your code to resolve that error message.

You might need to make that matrix "static", however. :)

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Thanks :)

My compiler gives this error when removing the *:

Untitled.cpp: In function 'int matrix()':
Untitled.cpp:13: error: invalid conversion from 'int*' to 'int'
Untitled.cpp:7: warning: address of local variable 'matrix' returned
Untitled.cpp: In function 'int main(int, char**)':
Untitled.cpp:22: error: invalid types 'int[int]' for array subscript

phorce
Posting Whiz
362 posts since Jul 2011
Reputation Points: 31
Solved Threads: 26
 
#include <iostream>

using namespace std;

int matrix()
{
	int matrix[10] = 
	{
		1, 1, 1, 1, 1,	
		1, 1, 1, 1, 1	
	};
	
	return *matrix;
	
}
int main(int argc, char *argv[]) {
	
	int static matr = matrix();	

	for(int i=0; (i < 10); i++)
	{
		cout << matr[i] << "\t";
		
	}
}


error: invalid types 'int[int]' for array subscript

When making it static

phorce
Posting Whiz
362 posts since Jul 2011
Reputation Points: 31
Solved Threads: 26
 

Did you remove the asterisk?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Yeahh, I removed the * and it displayed an error : invalid types 'int[int]' for array subscript

phorce
Posting Whiz
362 posts since Jul 2011
Reputation Points: 31
Solved Threads: 26
 

So, change the return type to a pointer after you make the matrix inside the function static.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

There are better ways of doing it, here is one examples.

typedef std::vector<int> Matrix; //alias

Matrix getMatrix(){
 return Matrix(10,1); //returns an array with 10 elements each initialized to 1
}
int main(){
 Matrix m = getMatrix();
 for(int i = 0; i < m.size(); ++i){
   cout << m[i] << endl;
 }
}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

Hello, basically I want to return an array from a function (So I don't output things in the class - just main)

But I seem to get the error: "invalid types 'int[int]' for array subscript"

Here is the code:

#include <iostream>

using namespace std;

int matrix()
{
	int matrix[10] = 
	{
		1, 1, 1, 1, 1,	
		1, 1, 1, 1, 1	
	};
	
	return *matrix;
	
}
int main(int argc, char *argv[]) {
	
	int matr = matrix();	

	for(int i=0; (i < 10); i++)
	{
		cout << matr[i] << "\t";		
	}
}

Any help would be great :)

Your code will compile if some changes are made in line 5, 13, 19 as shown below

#include <iostream>

using namespace std;

int* matrix()
{
        int matrix[10] =
        {
                1, 1, 1, 1, 1,
                1, 1, 1, 1, 1
        };

        return matrix;

}

int main(int argc, char *argv[]) {

        int* matr = matrix();

        for(int i=0; (i < 10); i++)
        {
                cout << matr[i] << "\t";
        }
}


But this is NEVER recommended and there are high chances it might not work. Why? Because the array is defined inside matrix(). So array will reside in the stack area of matrix(). When matrix() goes out of scope (after return to main function) the memory block where the array elements are stored maybe overwritten and hence data loss.

So when you want to return an array from a function either use the static way as suggested by thines01. Or you have to store the array in heap. So even if the function goes out of scope you wont lose your data. It will still be there in heap. But you have to make sure you delete those allocated memory after your use. Here is how you can do it. Note the delete statement at line 22.

#include <iostream>

using namespace std;

int* matrix()
{
        int* matrix = new int[10];
        for (int i = 0; i < 10; i++) matrix[i] = 1;

        return matrix;

}

int main(int argc, char *argv[]) {

        int* matr = matrix();

        for(int i=0; (i < 10); i++)
        {
                cout << *(matr+i) << "\t";
        }
        delete [] matr;
}


Also I have to make clear that the static solution and the heap solution are not same. Both have their own purpose. It's just that in your current case, both will work. Hope you are clear

subith86
Junior Poster
124 posts since Mar 2011
Reputation Points: 30
Solved Threads: 14
 

Your code will compile if some changes are made in line 5, 13, 19 as shown below

#include <iostream>

using namespace std;

int* matrix()
{
        int matrix[10] =
        {
                1, 1, 1, 1, 1,
                1, 1, 1, 1, 1
        };

        return matrix;

}

int main(int argc, char *argv[]) {

        int* matr = matrix();

        for(int i=0; (i < 10); i++)
        {
                cout << matr[i] << "\t";
        }
}

But this is NEVER recommended and there are high chances it might not work. Why? Because the array is defined inside matrix(). So array will reside in the stack area of matrix(). When matrix() goes out of scope (after return to main function) the memory block where the array elements are stored maybe overwritten and hence data loss.

So when you want to return an array from a function either use the static way as suggested by thines01. Or you have to store the array in heap. So even if the function goes out of scope you wont lose your data. It will still be there in heap. But you have to make sure you delete those allocated memory after your use. Here is how you can do it. Note the delete statement at line 22.

#include <iostream>

using namespace std;

int* matrix()
{
        int* matrix = new int[10];
        for (int i = 0; i < 10; i++) matrix[i] = 1;

        return matrix;

}

int main(int argc, char *argv[]) {

        int* matr = matrix();

        for(int i=0; (i < 10); i++)
        {
                cout << *(matr+i) << "\t";
        }
        delete [] matr;
}

Also I have to make clear that the static solution and the heap solution are not same. Both have their own purpose. It's just that in your current case, both will work. Hope you are clear

Thanks for your reply =)

It works, however, I'm working with matrix's (Sorry I forgot to mention) so I need to do a calculation..

cout << *(matr+i*rows+j) << "\t";


I've tried to do it, but it just adds them up and displays like 99, 100, 101 etc :(

phorce
Posting Whiz
362 posts since Jul 2011
Reputation Points: 31
Solved Threads: 26
 

Your code will compile if some changes are made in line 5, 13, 19 as shown below

#include <iostream>

using namespace std;

int* matrix()
{
        int matrix[10] =
        {
                1, 1, 1, 1, 1,
                1, 1, 1, 1, 1
        };

        return matrix;

}

int main(int argc, char *argv[]) {

        int* matr = matrix();

        for(int i=0; (i < 10); i++)
        {
                cout << matr[i] << "\t";
        }
}

But this is NEVER recommended and there are high chances it might not work. Why? Because the array is defined inside matrix(). So array will reside in the stack area of matrix(). When matrix() goes out of scope (after return to main function) the memory block where the array elements are stored maybe overwritten and hence data loss.

So when you want to return an array from a function either use the static way as suggested by thines01. Or you have to store the array in heap. So even if the function goes out of scope you wont lose your data. It will still be there in heap. But you have to make sure you delete those allocated memory after your use. Here is how you can do it. Note the delete statement at line 22.

#include <iostream>

using namespace std;

int* matrix()
{
        int* matrix = new int[10];
        for (int i = 0; i < 10; i++) matrix[i] = 1;

        return matrix;

}

int main(int argc, char *argv[]) {

        int* matr = matrix();

        for(int i=0; (i < 10); i++)
        {
                cout << *(matr+i) << "\t";
        }
        delete [] matr;
}

Also I have to make clear that the static solution and the heap solution are not same. Both have their own purpose. It's just that in your current case, both will work. Hope you are clear

Hi
why do not you get a open source matrix c++ library , and have him do all the job for you ???

some thing like, eigen, HASEM, Armadillo and so on.

hope it helps

therockon7throw
Newbie Poster
15 posts since Feb 2012
Reputation Points: 10
Solved Threads: 2
 

Hi why do not you get a open source matrix c++ library , and have him do all the job for you ???

some thing like, eigen, HASEM, Armadillo and so on.

hope it helps

Ahh, Hello.

I'm not allowed to use open source libraries (It's an assignment).. I'm making my own library though, hence, I'm not displaying anything in the class.

phorce
Posting Whiz
362 posts since Jul 2011
Reputation Points: 31
Solved Threads: 26
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You