Answers for "return array in c"

C
2

return array in c

// fill a preallocated buffer provided by 
// the caller (caller allocates buf and passes to the function)
void foo(char *buf, int count) {
    for(int i = 0; i < count; ++i)
        buf[i] = i;
}

int main() {
    char arr[10] = {0};
    foo(arr, 10);
    // No need to deallocate because we allocated 
    // arr with automatic storage duration.
    // If we had dynamically allocated it
    // (i.e. malloc or some variant) then we 
    // would need to call free(arr)
}
Posted by: Guest on June-17-2021
0

return array in c

void return_array(char *arr, int size) {
    for(int i = 0; i < size; i++)
        arr[i] = i;
}

int main() {
  	int size = 5;
    char arr[size];
    return_array(arr, size);
}
Posted by: Guest on June-17-2021

Code answers related to "C"

Browse Popular Code Answers by Language