Hi, I'm really confused now about how to return an int[] from a C++ function. Inside the function, I need to dynamically resize the array, but at the end I want the variable returned to be an array, not a pointer, but I have no idea how to do this:

int findAll( string str, string sub ) {
	int count = 0;
	int *indices = new int[ count ];
	for( int i = 0; i < str.length(); i ++ ) {
		if( str.find( sub, i ) == i ) {
			count += 1;
			int *temp = new int[ count ];
			for( int j = 0; j < count; j ++ )
				temp[ j ] = indices[ j ];
			delete [] indices;
			indices = temp;
		}
	}
	return indices;
}

The problem is that this gives me the error: invalid conversion from `int*' to `int' How do I return the actual array, rather than the pointer? Thanks!

you could use a vector instead of an array

#include <vector>

then you can return a vector<int> and not have to worry about every saying pointer!

Dave

Comments
Definitely the best idea

c++ regard arrays just as pointers.
you'd better use more complex datastruct if some other information like the size of the array is needed.

yea, vector's have a size() function

vector<int> A(5); //declare a vector of length 5
cout << A.size() << endl;

Ok, these are great suggestions, but just out of curiosity, why must C++ be so difficult/strict with this? My favourite language by far is Python, partly due to how utterly simple arrays and associative arrays (dictionaries) are. They can even contain different data types in different indices, making them so versatile. Just still a little confused as to why C++ is so, for lack of a better word, unhelpful when it comes to arrays. Please note, there most likely IS a good reason, just I am ignorant of it at the moment as I'm still fairly new to C++ after spending years with Python.

> why must C++ be so difficult/strict with this?
Power comes with responsibility.

Scripting languages have expressive power in that you can just write your solution without any care about how it works underneath. But your "I'll just return an array" will entail a hell of a lot of work for the machine, which you just won't see. In a small program, you won't notice, but sooner or later you might think "this is running a bit slow".

C++ (and down through C and to ASM) confront you head on with the problem to make you realise that it isn't quite a free lunch, and you need to start thinking about exactly how it's implemented. Yes it's more work for you, but get it right and you'll see some pretty amazing performance results.

The C++ STL (vector as already mentioned, and a lot of other goodies) go a long way to insulating you from some of the very raw detail. But vanilla arrays in C++ are the same as arrays in C, so you really have to do all the work yourself.

Ok, thanks! I had guessed through C++ 'strictness', there was a boost in performance, as opposed to something like Python (or Ruby, etc.) which is highly interpreted. I think I'll go with the vector method of doing this, as I need to do some serious reading up on the workings of arrays in C++ before attempting to use them extensively. Thanks for the help!

Arrays are useful because they help you understand how things work a little bit, but seem very unpractical to me now that STL has come around and has such a nice vector class.

Dave

This question has already been answered. Start a new discussion instead.