如何在C中替换索引处的1个数组值
我正在尝试创建一个扫雷游戏。为了显示一个标志已经放置在那个位置,我想在那个插槽中放置一个4。我不太确定如何更新数组中的1个元素,我在网上找到的所有信息都在整个数组中循环并更改了值。有什么办法可以这样做吗? 我遇到的具体问题是:如何在C中替换索引处的1个数组值,c,arrays,global-variables,C,Arrays,Global Variables,我正在尝试创建一个扫雷游戏。为了显示一个标志已经放置在那个位置,我想在那个插槽中放置一个4。我不太确定如何更新数组中的1个元素,我在网上找到的所有信息都在整个数组中循环并更改了值。有什么办法可以这样做吗? 我遇到的具体问题是: (*arr)[tempH][tempV] = 4; 其余的在这里 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h>
(*arr)[tempH][tempV] = 4;
其余的在这里
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int * arr;
void initialization(int vf, int hf) {
printf("%s\n", "Setting up the game");
int i, j, count = 0; // fills the board with a random sequence of 1's and 0's
for (i = 0; i < hf; i++) //representing bombs and empty spaces
for (j = 0; j < vf; j++)
*
(arr + i * vf + j) = (rand() % 2);
//0 is nothing 1 is bombs
for (i = 0; i < hf; i++) {
for (j = 0; j < vf; j++) {
printf("%d ", *(arr + i * vf + j));
if (j == hf - 1) {
printf("\n"); //creates new line at the end of row
}
}
}
printf("%s\n", "1 contains a bomb, 0 does not."); //game setup
}
void teardown() {
printf("%s\n", "Destroying the game"); //game destroy
}
void input(char select, char * command, int * coord_h, int * coord_v, int * stop) {
const int tempH = * coord_h; //creates temporary holders for coords
const int tempV = * coord_v;
printf("%s\n", "[F]Flag ");
printf("%s\n", "[R]Remove Flag ");
printf("%s\n", "[A] Assert a spot is mine free ");
printf("%s\n", "[Q] Quit");
scanf(" %c", & select);
if (select == 'f') {
printf("Please enter a horizontal coordinate: "); //selects a coordinate
scanf(" %d", coord_h);
printf("Please enter a vertical coordinate: "); //selects a coordinate
scanf(" %d", coord_v); &
(*arr)[tempH][tempV] = 4;
//TODO: REST OF LETTERS
} else if (select == 'r') {
} else if (select == 'a') {
printf("Please enter a horizontal coordinate: "); //selects a coordinate
scanf(" %d", coord_h);
printf("Please enter a vertical coordinate: "); //selects a coordinate
scanf(" %d", coord_v);
} else if (select == 'q') {
* stop = 1; //Stops loop in main
} else {
}
}
void update(int * coord_h, int * coord_v, int * type, int ** ptr, int hf, int vf) {
const int tempH = * coord_h; //creates temporary holders for coords
const int tempV = * coord_v;
printf("%d\n", ptr[tempH][tempV]); //testing values
if (ptr[tempH][tempV] == 1) { //checks to see if array at that point is a bomb
* type = 1; //returns type for display (bomb)
} else {
* type = 0; //returns type for display(not bomb)
}
int i, j, count = 0; // fills the board with a random sequence of 1's and 0's
for (i = 0; i < vf; i++) { //representing bombs and empty spaces
//memcpy(&updateArr[i], &arr[i], sizeof(int));
}
for (i = 0; i < hf; i++) {
for (j = 0; j < vf; j++) {
printf("%d ", *(arr + i * vf + j));
if (j == hf - 1) {
printf("\n"); //creates new line at the end of row
}
}
}
}
void display(int * type, int ** ptr, int hf, int vf) {
if ( * type == 1) { //takes type from update and displays
printf("%s\n", "BOOM");
} else if ( * type == 0) {
printf("%s\n", "OK");
}
}
int main() {
srand(time(NULL)); //creates random seed for bomb placement
int * stop = 0;
char select;
int * displayType;
char command[100];
int v, h; //board sizing
int coord_h, coord_v; //user inputed coords
printf("%s\n", "Enter horizontal board length: ");
scanf("%d", & h);
printf("%s\n", "Enter vertical board length:");
scanf("%d", & v);
const int vf = v,
hf = h; //sets constants for board so the array can use them
arr = (int * ) malloc(hf * vf * sizeof(int)); //dynamic memory allocation for board 2d array
int ** ptr = & arr;
initialization(vf, hf);
while (stop == 0) {
input(select, command, & coord_h, & coord_v, stop);
update( & coord_h, & coord_v, displayType, ptr, hf, vf);
display(displayType, ptr, hf, vf);
}
teardown();
}
#包括
#包括
#包括
#包括
int*arr;
无效初始化(int vf、int hf){
printf(“%s\n”,“设置游戏”);
int i,j,count=0;//用1和0的随机序列填充电路板
for(i=0;i
问题是您正在将arr
初始化为1D数组,并且希望像其2D数组一样访问它。基本上,arr
只是指向内存中连续的hf*vf
int变量的第一个索引的指针。如果您想要一个实际的2D数组,您需要将arr
更改为指向指针的指针(int**
),然后您可以像arr[i][j]
一样访问它。或者,您可以像这样保存它(1D数组),并实现一个函数,该函数获取arr、i、j和新值作为参数,然后更新*(arr+i*vf+j)
提示:在初始化中*(arr+i*vf+j)=(rand()%2)代码>来设置电路板。顺便说一句,这段代码有很多错误(空指针、未初始化的变量等),基本上需要完全重写才能正确,这太宽泛了。是的,我实际上正在重写它,但我认为拥有我正在研究的东西会更有益。初始化如何影响在索引处准确更改变量的能力?因为初始化也在索引到数组中,所以您显然已经知道如何进行。