how to return a string array from a function

后端 未结 9 695
暖寄归人
暖寄归人 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:13

    char *f()
    {   
        static char str[10][20];
    
        // ......
    
        return (char *)str;
    }
    
    int main()
    {
    
        char *str;
        str = f();
    
        printf( "%s\n", str );
    
        return 0;
    }
    

    You can use static instead of malloc. It's your choice.

提交回复
热议问题