How to use array parameter with static in function using C

1 Answer

0 votes
#include <stdio.h>
 
void f(double a[static 10], double b[static 10]) {
    for (int i = 0; i < 10; i++) {
        a[i] = i * 2;
        b[i] = a[i] + 1;
    }
}
 
int main(void)
{
    double a[10] = {0}, b[20] = {0};
    f(a, b); 
     
    double c[5] = {0};
    // f(b, c); // undefined behavior: array argument is too small
     
    for (int i = 0; i < 10; i++) {
        printf("%lf %lf\n", a[i], b[i]);
    }
}
   
   
   
/*
run:
   
0.000000 1.000000
2.000000 3.000000
4.000000 5.000000
6.000000 7.000000
8.000000 9.000000
10.000000 11.000000
12.000000 13.000000
14.000000 15.000000
16.000000 17.000000
18.000000 19.000000
   
*/

 



answered Mar 9, 2024 by avibootz
edited Mar 9, 2024 by avibootz
...