簡體   English   中英

如何使用指向指針的指針指向指向此鏈接列表的指針的項目

[英]How to use pointer to pointer pointers to pointers insert item in this linked list

美好的一天,我正在研究一款填充游戲,其中:兩個玩家通過放置在板上來獲得積分,一個又一個地是由游戲大師獲得的游戲作品(以可執行的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:
**

猜猜您要訪問2D數組中的項目。

在C語言中,2D和1D數組之間沒有什么不同,它們都是內存序列,只是看待它們的方式幾乎沒有什么不同。

例如

#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;
}

您可以閱讀以下地圖,

#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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM