Returning Arrays/Pointers from a function

前端 未结 7 1298
醉梦人生
醉梦人生 2020-11-28 12:07

I am trying to create a new integer array which is derived from a string of characters. For example :

char x[] = \"12334 23845 32084\";  

int y[] = { 12334,         


        
7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 12:52

    You can wrap an array in a structure and then return an instance of the structure. I'm mentioning this for completeness, it's not really something you'd want to do as it's ugly and there are better alternatives.

    #include 
    
    struct retval
    {
        int a[10];
    };
    
    struct retval test()
    {
        struct retval v = {{1, 5, 6}};
        return v;
    }
    
    int main()
    {
        struct retval data = test();
        printf("%d %d\n", data.a[1], data.a[2]);
    }
    

提交回复
热议问题