c - 使用函数初始化数组

标签 c

我试图让一个数组在一个函数中被初始化。按预期工作时,初始化数组应包含一组在一定范围内的随机数。这在 C 中可能吗?

最佳答案

 #include <time.h>  //time func for seed init
 #include <stdlib.h> //srand/rand funcs
 #include <stdio.h>  //Library for printf function
 #define LIMIT 100 

 void myFunction(int array[], int arrayIndexMaxSize){
     for (int index = 0; index < arrayIndexMaxSize; index++){
         array[index] = rand() % LIMIT;   //Where limit is randomly produced from 0 - Limit inclusive
         printf("array[%d] of arrayIndexMaxSize(%d) = %d\n", index, arrayIndexMaxSize, array[index]);  //prints out th$
     }
 }

 void main(){
     srand(time(0));  //initializes kinda random number generator
     int array[50]; //If known at runtime or compile time, must be at block scope meaing inside {curly} braces of any$
     myFunction(array, 50);
 }

关于c - 使用函数初始化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58421408/

相关文章:

c - 如何从exec返回控制权?

c - 用c语言播放.wav文件

c - 在函数中取消引用指针的指针(但不在主函数中)

c++ - 用于嵌入式项目的 C/C++ HTTP 客户端库

c - 如何让 LeakSanitizer 忽略程序泄漏的结尾

c - 打开和关闭 GLFW 窗口后无法获取控制台输入

c - 以数字作为变量的字符串,Unix C 问题

c - 显示链接列表时出错

c - 如何检查一个值是否具有偶校验位或奇校验位?

c - 相当于 Windows 中的 "sysconf(_SC_CLK_TCK)"