C-随机数数组功能 [英] C - random numbers array function

查看:46
本文介绍了C-随机数数组功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个函数,该函数创建大小为n且内部存储有随机数的数组,我一直在尝试几种选择,但是一旦我在main中使用该数组,则只有第一个数字可以正确显示.>

我正在使用CodeBlock.

  static int * arr;int,数字;printf(输入数组的长度:");scanf(%d/n",& num);arr = get_random_arr(num);for(i = 0; i< i ++){printf(函数外:%d \ n",*(arr + i));}返回0;int * get_random_arr(int num){int temp_arr [num];我srand((unsigned)time(NULL));对于(i = 0; i< i ++){temp_arr [i] = rand()%1001;printf(在函数内部:%d \ n",temp_arr [i]);}返回temp_arr;} 

这是编译的代码:

 输入数组的长度:3函数内部:224函数内部:774内部功能:60功能外:224功能外:2686648功能外:1977872892 

感谢"coderredoc",他的回答是我正在寻找的解决方案

解决方案

  scanf(%d/n",& num); 

您必须输入数字,后跟/ n .这不是您想要的.你想要这个

  scanf(%d \ n",& num); 

但是这里 scanf 读取num中的数字,但是读取继续进行,直到找到非空格字符后跟 \ n 为止.您在这里不需要 \ n .

还返回 return temp_arr; 并在其范围之外使用它是UB.

数组 temp_arr 具有自动存储持续时间,这意味着其寿命将在函数结束后结束.在其范围之外访问它是未定义的行为,这正是您所做的.

类似这样的东西

  #include< stdio.h>#include< stdlib.h>#include< time.h>int * get_random_arr(int num){if(num< = 0){fprintf(stderr,%s \ n",数字错误:num> = 1");出口(1);}int * temp_arr =(int *)malloc(sizeof * temp_arr * num);if(!temp_arr){fprintf(stderr,%s \ n","malloc中的错误");出口(1);}srand((unsigned)time(NULL));for(size_t i = 0; i< i ++){temp_arr [i] = rand()%1001;printf(在函数内部:%d \ n",temp_arr [i]);}返回temp_arr;}int main(){static int * arr;整数printf(输入数组的长度:");if(scanf(%d",& num)!= 1){fprintf(stderr,%s \ n",输入错误");出口(1);}arr = get_random_arr(num);for(size_t i = 0; i< i ++){printf(函数外:%d \ n",*(arr + i));}自由(arr);返回0;} 

代码试图显示一种实现所需目标的可能方法.动态分配的内存生存期超出了功能范围.这就是为什么它起作用.请注意 free()-用于释放动态分配的内存.

I'm trying to create a function that creates an array in size n with random numbers stored inside, I've been trying several option but once i use the array in main, only the first number shows up correctly.

I'm using CodeBlock by the way.

static int *arr;
int i, num;
printf("enter the length of the array: ");
scanf("%d/n", &num);
arr = get_random_arr(num);

for(i=0;i<num;i++)
{
    printf("outside the function: %d\n", *(arr+i));
}

return 0;

int *get_random_arr(int num)
{
    int temp_arr[num];
    int i;
    srand((unsigned)time(NULL));

    for (i=0;i<num;i++)
    {
        temp_arr[i] = rand() % 1001 ;
        printf("inside the function: %d\n",temp_arr[i]);
    }

    return temp_arr;
}

and this is the code that compiles:

enter the length of the array: 3
inside the function: 224
inside the function: 774
inside the function: 60
outside the function: 224
outside the function: 2686648
outside the function: 1977872892

Thank's to "coderredoc" his answer was the solution i was looking for

解决方案

scanf("%d/n", &num);

You have to enter the number followed by / and n. This is not what you want. You wanted this

scanf("%d\n", &num);

But here the scanf reads the number in num but then the reading continues until nonwhitespace character followed by \n is found. You don't need the \n here.

Also returning return temp_arr; and using it outside it's scope is UB.

The array temp_arr has automatic storage duration meaning its lifetime will end after the function ends. Accessing it outside its scope is undefined behavior which is precisely what you did.

Something like this

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int *get_random_arr(int num)
{
    if( num <= 0){
        fprintf(stderr, "%s\n", "Error in the number: num >= 1");
        exit(1);
    }
    int *temp_arr = (int*)malloc( sizeof *temp_arr *num);
    if( !temp_arr ){
        fprintf(stderr, "%s\n", "Error in malloc");
        exit(1);
    }

    srand((unsigned)time(NULL));

    for(size_t i=0;i<num;i++)
    {
        temp_arr[i] = rand() % 1001 ;
        printf("inside the function: %d\n",temp_arr[i]);
    }
    return temp_arr;
}
int main(){
    static int *arr;
    int num;
    printf("enter the length of the array: ");
    if( scanf("%d", &num) != 1){
        fprintf(stderr, "%s\n","Error in input" );
        exit(1);
    }
    arr = get_random_arr(num);

    for(size_t i=0;i<num;i++)
    {
        printf("outside the function: %d\n", *(arr+i));
    }
    free(arr);

    return 0;
}

The code tries to show a possible way to achieve what you wanted. Dynamically allocated memories lifetimes extends beyond the scope of the function. That's why it works. Note the free() - which is used to free the dynamically allocated memory.

这篇关于C-随机数数组功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆