C 更新函数中的数组会导致指针丢失
我试图将我的代码划分为函数,我有一个“生活游戏”的示例代码。 然而,当我尝试在函数中初始化2D网格数组时,gnuplot没有给出有效的数据点错误 这些是在main之前声明的全局变量:C 更新函数中的数组会导致指针丢失,c,arrays,function,pointers,global,C,Arrays,Function,Pointers,Global,我试图将我的代码划分为函数,我有一个“生活游戏”的示例代码。 然而,当我尝试在函数中初始化2D网格数组时,gnuplot没有给出有效的数据点错误 这些是在main之前声明的全局变量: static char **currWorld=NULL, **nextWorld=NULL 这是初始化游戏网格的原始逻辑,但这部分是主要的 if (game == 0){ // Use Random input for(i=1;i<nx-1;i++){ for(j=1;j<n
static char **currWorld=NULL, **nextWorld=NULL
这是初始化游戏网格的原始逻辑,但这部分是主要的
if (game == 0){ // Use Random input
for(i=1;i<nx-1;i++){
for(j=1;j<ny-1;j++) {
currWorld[i][j] = (real_rand() < prob);
population[w_plot] += currWorld[i][j];
}
}
}
else if (game == 1){ // Block, still life
printf("2x2 Block, still life\n");
int nx2 = nx/2;
int ny2 = ny/2;
currWorld[nx2+1][ny2+1] = currWorld[nx2][ny2+1] = currWorld[nx2+1][ny2] = currWorld[nx2][ny2] = 1;
population[w_plot] = 4;
}
else if (game == 2){ // Glider (spaceship)
printf("Glider (spaceship)\n");
// Your code codes here
}
else {
printf("Unknown game %d\n",game);
exit(-1);
}
游戏的值是0,prob是0.2,我测试了内存分配和其他东西,它们工作正常。
唯一的区别是我把逻辑移到了一个函数。这怎么会发生?我的数组是全局数组,我无法理解它如何无法初始化
这是gnuplot错误:
Skipping data file with no valid points
由于gnuplot和其他函数工作正常,我没有添加它们,但如果您需要任何信息,我可以添加
以下是文件本身的链接:
在您的函数中,
nx
似乎不知从何而来。能否显示整个代码?nx是arraysYes的列和行大小,但它们可能是全局变量,初始化为0,以后在main中定义或不定义,这可能会导致问题。如果没有完整的代码,很难说什么。你是对的,我在文件中添加了一个链接,顺便说一句,MeshPilot没有问题,我可以正确模拟。在问题本身中提供一个,而不是作为链接。
init_game(game, prob);
Skipping data file with no valid points