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 :)

Recommended Answers

All 11 Replies

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. :)

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

#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

Did you remove the asterisk?

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

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

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

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

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 :(

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.