如何在C中的索引处替换1个数组值

问题描述 投票:0回答:1

我正在尝试创建扫雷游戏。为了表明在该位置已放置一个标志,我想在该插槽中放置一个4。我不太确定如何更新数组的1个元素,我在网上发现的所有信息都遍历整个数组并更改了值。反正有这样做吗?我遇到麻烦的特定行是:

(*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();
}
c arrays global-variables
1个回答
1
投票

问题是您将arr初始化为1D数组,并且希望像2D一样对其进行访问。基本上,arr只是指向在内存中连续的hf * vf int变量的第一个索引的指针。如果要使用实际的2D数组,则需要将arr更改为指向指针(int **)的指针,然后可以像arr[i][j]一样访问它。另外,您可以像这样保持它(一维数组),并实现一个将arr,i,j和新值作为参数然后更新*(arr + i * vf + j)的函数。

© www.soinside.com 2019 - 2024. All rights reserved.