How can we pass an array directly to a function in C?
For example:
#include <stdio.h>
void function(int arr[]) {};
int main(void) {
int nums[] = {3, -11, 0, 122};
function(nums);
return 0;
}
Instead of this, can we just write something like function({3, -11, 0, 122});
?
You can make use of a compound literal. Something like
function((int []){3, -11, 0, 122});
You could pass array as compound literal as below.
function((int []){3, -11, 0, 122});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With