더블포인터 동적할당에 대해서 정말 이해 안가는 부분이 있습니다.
글쓴이: jongyoungcha / 작성시간: 목, 2012/12/13 - 11:11오후
이클립스 CDT환경으로 지뢰찾기 게임을 만들어보고 있는데 정말 의아한 부분이 있어서 이렇게 질문을 올리려고합니다.
#include <stdio.h> #include <stdlib.h> typedef struct cell{//셀 int key; int wid; int high; struct cell *top_left; struct cell *top; struct cell *top_right; struct cell *left; struct cell *right; struct cell *bottom_left; struct cell *bottom; struct cell *bottom_right; }Cell; typedef struct table{//게임의 테이블 int wid; int high; Cell** table_list; }Table; Cell** make_table(int* high, int* wid); int input_cell(int y, int x, Table* table, Cell *cell); int main() { int i,j; int high, wid; Table *table; Cell cell; i = input_cell(2, 2, &table, &cell); return 0; } Cell** make_table(int* high, int* wid){// 테이블을 만드는 함수 int i;//루프를 돌기위한 정 Table* table; table = (Table*)malloc(sizeof(Table)); printf("만들 테이블을 크기 입력해 주세요(넓이, 높이) :"); scanf("%d %d", &table->high, &table->wid); table->table_list = (Cell**)malloc(sizeof(Cell)*table->high); for(i=0; i<table->high; i++) { table->table_list[i] = (Cell*)malloc(sizeof(Cell)*(table->wid)); ///////////////////////////// 바로 이부분입니다.////////////////////////////////////// } return table; } int input_cell(int y, int x, Table* table, Cell *cell)// 셀을 테이블에 집어넣는 함수 { printf("into input_celll\n"); cell->wid = x; cell->high = y; cell->top_left = &(table->table_list[y-1][x-1]); return 0; }
이 코드에서 에러는 현제 없습니다만은....
table->table_list[i] = (Cell*)malloc(sizeof(Cell)*(table->wid));
이부분을 다음과 같이 고치면 에러가 발동합니다.
table->*(table_list+i) = (Cell*)malloc(sizeof(Cell)*(table->wid));
에러메시지는 ../minesweeper.c:54: error: expected identifier before '*' token 다음과 같네요
원래 제가 알기로는 arr[i] == *(arr+i) 으로 알고있는데 왜 에러가 나는 것일까요... 정말 궁굼합니다
Forums:
대충 눈에 보이는 거만
대충 눈에 보이는 거만 적자면,
table->table_list[i] 와 동치는 *(table->table_list + i) 입니다. 마지막 문장에서 arr에 대응하는 것이 table->table_list니까요.
그리고 중간에,
조언 정말 감사합니다. 가르침 정말 감사합니다.
조언 정말 감사합니다. 가르침 정말 감사합니다. 복받으실꺼에요 ^^
잘배워갑니다.
댓글 달기