Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在函数中创建数组并在c中返回_Arrays_C_Pointers - Fatal编程技术网

Arrays 如何在函数中创建数组并在c中返回

Arrays 如何在函数中创建数组并在c中返回,arrays,c,pointers,Arrays,C,Pointers,我需要做一个函数,创建一个具有随机大小和随机字符的int数组,然后返回它 因为数组的大小未知,所以我无法将其设置为全局变量,所以为此我创建了一个单独的函数。然后我尝试使用指针返回数组,以便在主函数中使用它将int数组作为字符打印 这是我到目前为止的代码,但它给了我一个错误: '数组初始值设定项必须是初始值设定项列表或宽字符串文字' int length; void set_length () { //generates random integer between 8 and 15 w

我需要做一个函数,创建一个具有随机大小和随机字符的int数组,然后返回它

因为数组的大小未知,所以我无法将其设置为全局变量,所以为此我创建了一个单独的函数。然后我尝试使用指针返回数组,以便在主函数中使用它将int数组作为字符打印

这是我到目前为止的代码,但它给了我一个错误: '数组初始值设定项必须是初始值设定项列表或宽字符串文字'

int length;

void set_length () {
    //generates random integer between 8 and 15 which is used as the length of the array[]
    length = (rand() % 8 + 8);
}

int * ascii () {

    int array[length];

    //this is a function that assigns the values into the array
    random_values(array, length);

    //just to check
    for (int i = 0; i < length; i++) {
        printf("%c", array[i]);
    }

    return array;

}


int main () {

    //to prevent rand() from producing the same value every time
    srand(time(NULL));

    set_length();

    int password[] = * ascii();


    //what i want from this code but instead gives me the error
    for (int i = 0; i < length; i++) {
        printf("%d", password[i]);
    }
}

int长度;
空集长度(){
//生成8到15之间的随机整数,用作数组的长度[]
长度=(rand()%8+8);
}
int*ascii(){
整数数组[长度];
//这是一个将值赋给数组的函数
随机_值(数组、长度);
//只是检查一下
for(int i=0;i

任何帮助都将不胜感激。谢谢。

您不能从函数返回具有自动存储持续时间的本地阵列,因为它在退出函数后将不活动

而且这个宣言

int password[] = * ascii();
这是错误的。不能使用标量值初始化数组,因为在任何情况下,
int*
类型的指针都指向单个对象

因此,您需要动态分配一个数组

比如说

int *array = malloc( length * sizeof( int ) );
此外,使用全局变量
length
也是一个坏主意。您可以将其作为函数
ascii
的参数

反过来,函数
set_length
可以返回长度的计算值。

\include
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int length;

void set_length () {
    //generates random integer between 8 and 15 which is used as the length of the array[]
    length = (rand() % 8 + 8);
}
// Here i add new variable to your function double pointer
int ascii (int **pass)
{

    int array[length];

    //this is a function that assigns the values into the array
    // Change this whith generate random that you use : random_values(array, length);
    for (int i = 0; i < length; i++)
    {
        array[i] = i;
    }
    

    //just to check
    for (int i = 0; i < length; i++) 
    {
        printf(" random number is %d \n", array[i]);
    }
    // save your data in new pointer that we passed to the function
    *pass = array;
    return 0;
}


int main () {

    //to prevent rand() from producing the same value every time
    srand(time(NULL));

    set_length();

    int *password = malloc(sizeof(int) * length);
    // Here we pass adress of pointer
    ascii(&password);


    //what i want from this code but instead gives me the error
    for (int i = 0; i < length; i++) {
        printf(" Sheck : %d\n", password[i]);
    }
}
#包括 #包括 整数长度; 空集长度(){ //生成8到15之间的随机整数,用作数组的长度[] 长度=(rand()%8+8); } //在这里,我向函数双指针添加新变量 int ascii(int**pass) { 整数数组[长度]; //这是一个将值赋给数组的函数 //将此更改为生成您使用的随机值:随机_值(数组、长度); for(int i=0;i
这是有效的。非常感谢你!我发现你写的代码还有一个很奇怪的问题。我使用random_values()函数只指定33到126之间的int值。当我使用您的代码时,这是有效的,但当我注释掉“只是为了检查”部分时,它就不起作用了。我猜这是一个记忆问题,我真的什么都不知道。为什么会发生这种情况?这一行的字符
*pass=array?不,我没有。我在代码中使用了rand()%94+33,但它给我的值是387。随机函数或内存有问题吗