如何使用指向指针的指针指向指向此链接列表的指针的项目

时间:2018-08-21 12:22:46

标签: c pointers

早安, 我正在研究一款填充游戏,其中:两个玩家通过放置在板上一个接一个地获得游戏大师获得的游戏作品(以可执行Ruby程序的形式)来获得积分。当无法再放置游戏块时,游戏结束。

下面是用于读取我的玩家编号和起始片段的代码:

***void init_player(t_player *player)***
{
    char    *line;

    get_next_line(0, &line);
    if ((!(ft_strncmp(line, "$$$ exec p", 10))))
    {
        if (line[10] == '1')
        {
            player->id = '1';
            player->my_shape = 'O';
            player->current_shape = 'o';
        }
        else
        {
            player->id = '2';
            player->my_shape = 'X';
            player->current_shape = 'x';
        }
        ft_strdel(&line);
        return ;
    }
    return ;
}
int     main(void)
{
    t_player    *me;
    me = (t_player *)malloc(sizeof(*me));

    init_player(me);
    ft_putchar(me->my_shape);
    ft_putchar('\n');
    return (0);
}

现在,我需要通过创建指向大小为n + 1且n为15的指针的指针来读取地图大小,请参见下面的地图。或者,我可以尝试其他可以建议的方法。谢谢 检查下面的地图

$$$ exec p1 : [players/abanlin.filler]
***Plateau 15 17:***
    01234567890123456
000 .................
001 .................
002 .................
003 .................
004 .................
005 .................
006 .................
007 .................
008 ..O..............
009 .................
010 .................
011 .................
012 ..............X..
013 .................
014 .................
Piece 1 2:
**

2 个答案:

答案 0 :(得分:0)

猜想您要访问2D数组中的项目。

在C语言中,二维数组和一维数组之间没有什么不同,它们都是内存序列,只是它们的查看方式几乎没有差异。

例如

#include <stdlib.h>
#include <assert.h>

typedef struct {
  // Here mem can be used like a width * height 2D array
  // You can change 'char' here to whatever type you want
  char *mem;
  size_t width;
  size_t height;
} MyMap;

MyMap *create_map(size_t width, size_t height) {
  assert(width > 0 && height > 0);
  // As the size of 'char' is 1 byte, mem should take
  // (width * height * 1) bytes from memory. If you changed
  // type of mem, the size should be 'width * height * sizeof(NewType)'
  MyMap *self = (MyMap *)malloc(sizeof(MyMap) + width * height);
  assert(self);
  self->mem = (char *)(self + 1);
  self->width = width;
  self->height = height;
  return self;
}

void delete_map(MyMap **self){
  if (self && *self) {
    free(*self);
    *self = NULL;
  }
}

/**
 * @param x must be between 0 and width
 * @param y must be between 0 and height
 */
void set_map(MyMap *self, size_t x, size_t y, char value) {
  assert(self);
  assert(x < self->width && y < self->height);
  size_t index = self->width * y + x;
  self->mem[index] = value;
}

int main () {
  const size_t height = 15;
  const size_t width = height + 1;
  MyMap *map = create_map(width, height);
  set_map(map, 0, 0, 'X');
  delete_map(&map);
  return 0;
}

答案 1 :(得分:0)

您可以阅读以下地图,

#include <stdio.h>
#include <stdlib.h>
#define ROW 5
#define COL 5

int main() {
    int i,j;
    char **board;
    if((board = (char **)calloc(sizeof(char *) , ROW)) == NULL){
        printf("calloc failed at memory allocation for dptr\n");
    }
    printf("Enter board of size %d x %d", ROW, COL);
    for(i = 0; i < ROW; i++){
        if((board[i] = (char *)calloc(sizeof(char) , COL)) == NULL){
            printf("calloc failed at memory allocation for ptr %d\n", i);
        }
        for(j = 0; j < COL; j++){
            if(scanf("%c",&board[i][j]) < 1){
                printf("scanf failed!");
            }
        }
    }
//printing board
    for(i = 0; i < ROW; i++){
        for(j = 0; j < COL; j++){
            printf("%c ",board[i][j]);
        }
        printf("\n");
    }
    for(i = 0; i < ROW; i++){
        free(board[i]);
    }
    free(board);
    return 0;
}

注意:您也可以采用木板用户的大小,而不是定义ROW和COL。