You are aware that when you use []s (empty or otherwise) for a parameter, it is just another way of writing a pointer? Such that if you try to compile both
1 2 3
|
void Xout(char message[], int length)
{
}
|
and
1 2 3
|
void Xout(char* message, int length)
{
}
|
the compiler will complain about a redefinition, whereas if they'd been distinct types, they would have been seen as different overloads. (And any value in the []s is just ignored.)
As the [] form acts like a pointer, I always write functions like this using the * form instead, to make this totally clear.
While you cannot return a char[], you can do this (with pointers.)
1 2 3 4 5 6 7 8
|
char* Xout(char* message, int length)
{
for(i = 0; i < length; i++)
{
message[i] = 'X';
}
return message;
}
|
An approach which is used by some C library functions, e.g. strcpy, strcat, ... (i.e. they return a pointer to the buffer you gave them in the first place.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream>
using namespace std;
char* Xout(char* message, int length);
int main()
{
const int LEN = 64;
char message[LEN];
cin.getline(message, LEN);
cout << Xout(message, LEN) << endl;
return 0;
}
char* Xout(char* message, int length)
{
for(int i = 0; i < length; i++)
{
message[i] = 'X';
}
return message;
}
|
Andy
PS Strictly speaking
void Xout(char message[], int length);
looks like
void Xout(char * const message, int length);
rather than
void Xout(char* message, int length);
but the const-ness of the pointer isn't enough to make these functions look like different overloads to the compiler.