Returning Array from function in C

前端 未结 5 400
醉话见心
醉话见心 2021-01-24 14:44

For some reason, my function is only returning the first element in my array and I cannot figure out why the rest of the array goes out of scope. The function takes two integer

5条回答
  •  醉梦人生
    2021-01-24 15:15

    You can't return a pointer to an automatic local variable. total is an automatic local variable and it doesn't exist after function body executed.

    Pointer to static local variable or dynamically allocated variable can be returned. Change
    int total[10]; to

    int *total = malloc(10 * sizeof(int));
    

提交回复
热议问题