Return array in a function

后端 未结 19 2787
清酒与你
清酒与你 2020-11-22 05:23

I have an array int arr[5] that is passed to a function fillarr(int arr[]):

int fillarr(int arr[])
{
    for(...);
    return arr;
         


        
19条回答
  •  借酒劲吻你
    2020-11-22 06:03

    to return an array from a function , let us define that array in a structure; So it looks something like this

    struct Marks{
       int list[5];
    }
    

    Now let us create variables of the type structure.

    typedef struct Marks marks;
    marks marks_list;
    

    We can pass array to a function in the following way and assign value to it:

    void setMarks(int marks_array[]){
       for(int i=0;i

    We can also return the array. To return the array , the return type of the function should be of structure type ie marks. This is because in reality we are passing the structure that contains the array. So the final code may look like this.

    marks getMarks(){
     return marks_list;
    }
    

提交回复
热议问题