1// fill a preallocated buffer provided by
2// the caller (caller allocates buf and passes to the function)
3void foo(char *buf, int count) {
4 for(int i = 0; i < count; ++i)
5 buf[i] = i;
6}
7
8int main() {
9 char arr[10] = {0};
10 foo(arr, 10);
11 // No need to deallocate because we allocated
12 // arr with automatic storage duration.
13 // If we had dynamically allocated it
14 // (i.e. malloc or some variant) then we
15 // would need to call free(arr)
16}
1void return_array(char *arr, int size) {
2 for(int i = 0; i < size; i++)
3 arr[i] = i;
4}
5
6int main() {
7 int size = 5;
8 char arr[size];
9 return_array(arr, size);
10}