Return array from function, how do I get the size?

Hello all, I realise this is a ridiculously simple question, but please humour me.

If I want to return an array from a function, I see I can dynamically create the array within the function, and return a pointer to the first memory location of that array. But how would I then determine the length of the array returned?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using std::cout;
using std::endl;

char* retArray(void)
{
	char* letters = new char[];
	letters = "abcdefgh";
	return letters;
}


int main(void)
{
	char* ptr = 0;
	ptr = retArray();

	int length = 8;
	// Outputing the array relies on me
        // knowing its length,
	// How would I determine this before hand?
	for(int i=0; i<length; i++)
	{
		cout << *(ptr+i);
	}
	cout << endl;
        delete [] ptr;
	return 0;
}


Many thanks for any help!
Last edited on
Line 6: (void) is an abomination (according to Stroustrup; I agree).
Line 8: you must provide a size for the array.
Line 9: you cannot copy arrays like that.
Try:
1
2
3
4
5
6
char* retArray()
{
	char* letters = new char[ 9 ];  // 8 characters + null terminator
	char_traits <char> ::copy( letters, "abcdefgh", 9 );  // same as strcpy()
	return letters;
}

Character arrays are typically terminated with a null character (a character with the value zero).

You could say:
cout << ptr;
or
1
2
	for (int i = 0; p[ i ] != 0; i++)
		cout << p[ i ];


Arrays of other types are typically returned via reference:
1
2
3
4
5
6
7
void retArray( (int*)& p, unsigned& size )
{
	size = 4;
	p = new int[ size ];
	for (int n = 1; n < 4; n++)
		p[ n ] = n;
}

:-)
Thank you, helps a lot.

Regarding line 6, and specifying function(void), I do remember reading that somewhere now (Bjarne's site possibly), but why is it an abomination, aren't you simply being clear in the fact that you intended for the function to have no arguments?

I was being a bit of a moron it seems, as cout << ptr; fulfils the problem, and I assume you could also strlen(ptr) if you needed it.

Hate it when I get mind blocks, thanks very much for the tips, very useful!
You could make life for you easier by using string...

and function(void) is somewhat like /* this comment intentioally has no meaning */
In C89 void foo() means a "function taking an unspecified number of arguments ..." But since you can't access unspecified arguments, it is pretty much the same as saying "takes no arguments" (usually).

In C99 that is no longer permissible (I think), and the specification prefers you to write void foo(void) to explicitly indicate that the function takes no arguments.

In C++ the standard states that all arguments must be explicitly indicated, either by name or by an elipsis (...), and that just saying 'void' all by itself is stupid (not in those exact words, though :-P). So saying void foo() you are explicitly stating that the function takes no arguments.

/* this comment brought to you by the letter after C */

:-)
Thanks guys,

I understand, last time I do that :)
Topic archived. No new replies allowed.