1

I do not understand why the following code:

char** receive_message(char** ret)
{
    char* temp = "text";

    strcpy(&ret, temp);
    return ret;
}

gives this error:

warning: passing argument 1 of ‘strcpy’ from incompatible pointer type [enabled by default]
     strcpy(&ret, temp);

I'm trying to copy a message that is generated inside a function to a char* array that is allocated outside the function.

Mari
  • 35
  • 3

4 Answers4

2

Use

strcpy(*ret, temp);

Instead of

strcpy(&ret, temp);

You need to deference char ** instead of taking a reference of it.

Andrej Adamenko
  • 1,650
  • 15
  • 31
1

It wants something of type char* whereas &ret is of type char***.

You could maybe pass it *ret which is of type char*.

chasep255
  • 11,745
  • 8
  • 58
  • 115
0

ret is a char** but strcpy expects a char*

You then take the address of &ret making it char***

Either pass ret as char* into receive_message or dereference it when passing it to strcpy (*ret, temp)

Josh
  • 73
  • 1
  • 5
0

You are passing strcpy the address of a pointer to a pointer to char... That's 2 levels of indirection too many. You do not need all these indirections, you can simplify receive_message this way:

char *receive_message(char *ret) {
    return strcpy(ret, "text");
}

Not really worth a separate function ;-)

It is useful to pass the address of the pointer to receive_function is you intend for this function to modify the pointer in the calling scope, as would be required if you need to reallocate the buffer, but since you do not pass the size, such reallocation would be hazardous.

chqrlie
  • 131,814
  • 10
  • 121
  • 189