Question

Say i have a char* method that returns a char array. How mould i set another char array equal to that return value? For example, here's the char* method:

char* work(int num){
    char buf[32];
    sprintf(buf, "%d", num);

    return buf;
}

what do I need to put in the main method to use the returned value? I've tried

int main() {
    char rValue[32] = work(5);
}

but i get "error: array initializer must be an initializer list or string literal"

and

int main() {
    char rValue[32];
    rValue = work(5);
}

gives me "error: array type 'char [32]' is not assignable".

I know that the solution is probably extremely simple, but i'm lost. How can i access the returned value?

Était-ce utile?

La solution 2

Firstly, you cannot return a local array like that. It will go out of scope and the values may become garbage. What you need to do is to dynamically allocate it first. Once you do that, you're safe to return it.

char* work(int num){
    char *buf = malloc(sizeof(char)*32);
    sprintf(buf, "%d", num);

    return buf;
}

Now, to receive a pointer, you'll need another pointer variable. Not an array.

int main() {
    char *rValue = work(5);
}

Finally, you need to free the memory you've allocated so that it can be used in future.

int main() {
    char *rValue = work(5);
    free(rValue);
    return 0;
}

See more about malloc and free.

Autres conseils

What you want to do is pass the rValue using a pointer.

I'm not entirely sure on what you are trying to achieve but my amendments below allow for compilation with no errors on gcc.

What you are doing is passing the address of the first element of your rValue array into work. There is no need for a return as you are directing working on the original data. This is similar to what some languages have when they "pass by reference"

#include <stdio.h>

void work(int num, char *rValue) {
    sprintf(rValue, "%d", num);
}

int main() {
    char rValue[32];
    work(5, rValue);
}

You cannot return an array from a function. That is because the array buf is local to the function work. The array buf goes out of scope after the function returns and accessing it thereafter invokes undefined behaviour.

Arrays are not first-class objects in C in the sense that you cannot pass an array to a function or cannot return an array from a function. What actually gets passed or returned is a pointer to the first element of the array. This is also stated as the array decays to a pointer to its first element.

You should allocate your array dynamically so that it is not destroyed until you free the memory allocated.

char *work(int num) {
    char *buf = malloc(32);

    // check the result of malloc
    // in case it fails
    if(buf == NULL) {
        printf("malloc failed to allocate memory\n");
        return NULL;
    } 

    // make sure that buff is large enough 
    // to contain num else sprint will overrun
    // buf causing undefined behaviour
    sprintf(buf, "%d", num);

    return buf;
}

And your main should be

int main(void) {
    char *p = work(5);
    if(p == NULL)
        return 1;

    printf("%s\n", p);
    free(p);
    return 0;
}

char buf[32]; is a local variable. its memory will be freed after function execution. You should use malloc to assign memory from the heap to a char pointer. You can copy arrays with memcopy. In case you want to copy one array to another work with char * not char (pointer).

#include <stdio.h>
#include <string.h>
#include <stdlib.h>  
char* work(int num){
    char *r = (char*)malloc(sizeof(char)*32);
    sprintf(r, "%d", num);
    return r;
}

int main() {
    char *rValue = NULL;
    rValue = work(5);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top