Pass and return a char array.

Jul 29, 2013 at 9:51pm
I am passing and returning a char array wrong. How do I make a function that takes in a char array, does something to it, and returns it.

Here is the code I have now, any way to make this function work?
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
#include <iostream>

using namespace std;

char[] Xout(char[], int);

int main(int argc, char* argv[])
{
    const int LEN = 64;
    char message[LEN ];

    cin.getline(message, LEN);
    message = Xout(message, LEN);
    cout << message;

    return 0;
}

char[] Xout(char message[], int length)
{
    for(i = 0; i < length; i++)
    {
        message[i] = 'X';
    }
    return message;
}


Code::Blocks gives this error.
1
2
3
4
5
C:\Users\[...]\stringTest\main.cpp|5|error: expected unqualified-id before '[' token|
C:\Users\[...]\stringTest\main.cpp||In function 'int main()':|
C:\Users\[...]\stringTest\main.cpp|13|error: 'Xout' was not declared in this scope|
C:\Users\[...]\stringTest\main.cpp|19|error: expected unqualified-id before '[' token|
||=== Build finished: 3 errors, 0 warnings (0 minutes, 0 seconds) ===|


I think I am passing it correctly, I just don't know how to return it.

Thanks
Jul 29, 2013 at 9:58pm
You don't need to return it.

Since an array is essentially a pointer, any operations performed in the function will affect the array you pass in.
Jul 29, 2013 at 10:00pm
It appears that you're trying to use arrays of char to represent strings. Why not just use strings?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

std::string Xout(const std::string& message)
{
    return std::string(message.size(), 'X');
}

int main()
{
    std::string message;
    getline(std::cin, message);
    message = Xout(message);
    std::cout << message << '\n';
}
Jul 29, 2013 at 10:10pm
@Cubbi, I am not using string because I am experimenting with char arrays.
@iHutch105, that makes sense. But what if I create an array of chars inside a function and need to return it.

Without making anything a "string", can I fix this function?
Jul 29, 2013 at 10:17pm
Functions cannot return C-style arrays. They can only modify them by reference or through pointers.
Jul 29, 2013 at 11:43pm
@Cubbi, yeah I made it a void and everything worked out. I did not know that you cannot return a char array.

Thanks.
Jul 29, 2013 at 11:59pm
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.
Last edited on Jul 30, 2013 at 12:13am
Topic archived. No new replies allowed.