Reading floats into an array

后端 未结 6 852
[愿得一人]
[愿得一人] 2021-01-22 00:39

How could I read let\'s say 10 floats and store them in an array without wasting any memory?

6条回答
  •  情书的邮戳
    2021-01-22 01:24

    float arr[10];
    for(i = 0; i < 10; i++){
        scanf("%f", &arr[i]);
    }
    

    I know, it's like the above, but doesn't use the extra int in case it's not optimized out. hehe

    To use even less memory us the code below. They're all read into an array, but only the last one to be read in is in the array:

    float arr[1];
    for(i = 0; i < 10; i++){
        scanf("%f", &arr[0]);
    }
    

提交回复
热议问题