Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
从C中的函数访问动态分配的数组时程序崩溃_C_Arrays_Dynamic_Structure_Allocation - Fatal编程技术网

从C中的函数访问动态分配的数组时程序崩溃

从C中的函数访问动态分配的数组时程序崩溃,c,arrays,dynamic,structure,allocation,C,Arrays,Dynamic,Structure,Allocation,感谢您抽出时间阅读此文章。 当我试图访问以前在函数中动态分配的数组时,我的程序就会崩溃。这里有一些代码 //function to allocate my array, gives an array as return Player* allocate(Player **a, int n) { *a = (Player *) malloc(sizeof(Player)*(n)); return *a; } //populating my allocated array, re

感谢您抽出时间阅读此文章。 当我试图访问以前在函数中动态分配的数组时,我的程序就会崩溃。这里有一些代码

//function to allocate my array, gives an array as return
Player* allocate(Player **a, int n) { 
    *a = (Player *) malloc(sizeof(Player)*(n));
    return *a;
}

//populating my allocated array, return an array
Player* initializePlayers(Player *a, int n){
    int i=0;
    char tmp[MAXCHAR];
    for(i=0; i<n; i++){
        printf("Insert player name %d\n", i);
        scanf("%s", tmp);
        strcpy(a[i].playerName,tmp);
        printf("Player %s assigned.\n", a[i].playerName);
        a[i].playerNumber=i;
    }
return a;
}

//setup function which includes both the above ones, called from main
void setup(Player *array, int *nPlayers){
    int done=0;
    while (done==0){
    printf("How many players?\n");
    scanf("%d", nPlayers);
    if (*nPlayers <2 || *nPlayers>8){
        printf("Choose between 2 and 8\n");
            waitFor(2);
            clear();
            done=0;
       }
    else done=1;
    }
    allocate(&array, *nPlayers);
    initializePlayers(array, *nPlayers);
}

我对编程相当陌生,因此我可能缺少一些非常明显的东西,请不要想当然地对函数
setup()
中复制的参数
array
进行修改不会影响函数
main()
中的局部变量。而你的程序恰好崩溃了

您的
setup()
应该是这样的:

void setup(Player **array, int *nPlayers){

    /* ... */

    allocate(array, *nPlayers);
    initializePlayers(*array, *nPlayers);
}
Player * array=NULL;
//I'm passing nPlayers because i want the value to be saved and available on my main
setup(&array, &nPlayers);
应该这样称呼它:

void setup(Player **array, int *nPlayers){

    /* ... */

    allocate(array, *nPlayers);
    initializePlayers(*array, *nPlayers);
}
Player * array=NULL;
//I'm passing nPlayers because i want the value to be saved and available on my main
setup(&array, &nPlayers);
请发布一份包含所有必需声明和定义的声明,包括
玩家的声明