how to return a string array from a function

后端 未结 9 649
暖寄归人
暖寄归人 2020-12-29 10:53
char * myFunction () {

    char sub_str[10][20]; 
    return sub_str;

} 

void main () {

    char *str;
    str = myFunction();

}

error:return

9条回答
  •  执笔经年
    2020-12-29 11:12

    Reason:
    you need the return type to be char(*)[20]. But even in this case you don't want to return a pointer to a local object from the function.
    Do:
    Use malloc to allocate sub_str, and return char**.

提交回复
热议问题