基于现有代码定义正确的结构

时间:2012-04-26 18:25:31

标签: c pointers struct

我有一些C结构的功课问题...如果有人可以提供帮助。因为我没理解。

工作代码包含那些位(这是一个带有头部包含方向的蛇的游戏):

game_t *game = …;

game->snake.head->direction = …;
snake_info(game->snake)
snake_destroy(&(game->snake));

原型(我显然无法改变):

void snake_info(snake_t const *snake);
void snake_destroy(snake_t *snake);

我正在努力构建的结构。

typedef struct game {
    snake_t snake; // to match call to snake_destroy and direction assignation
    // or
    snake_t *snake; // to match call to snake_info
} game_t;

但我不能同时兼顾两者。

编辑: struct被称为snake,而实际上是game。 我是否会错过一些明显的东西或这些原型(或电话)是否有问题?

编辑2:

第一个解决方案snake_t snakegame)中的编译器错误:

snake.c: In function ‘game_print’:
snake.c:244:5: erreur: incompatible type for argument 1 of ‘snake_info’
gamecore.c:20:6: note: expected ‘const struct snake_t *’ but argument is of type ‘snake_t’

编译器错误与第二个解决方案snake_t * snake`:

snake.c: In function ‘game_destroy’:
snake.c:205:5: attention : passing argument 1 of ‘snake_destroy’ from incompatible pointer type
gamecore.c:54:6: note: expected ‘struct snake_t *’ but argument is of type ‘struct snake_t **’

3 个答案:

答案 0 :(得分:0)

您的结构不正确。你有两个同名的名字'snake'

两者都必须在结构范围内具有唯一名称才能允许合法代码

答案 1 :(得分:0)

编译器错误说明了一切,你必须使用struct snake_t来定义你的结构:

typedef struct game {
    struct snake_t snake;
} game_t;

由于给定代码中的snake_t snake,您必须使用snake.head

答案 2 :(得分:-1)

使用指针的地方有点混乱。随着更多练习使用指针将变得更自然。

此外,使用不与结构名称冲突的变量名称。


取决于代码中指针的使用量。

对现有代码的最小编辑:

typedef struct game {
    snake_t snake; // A snake_t instance is created with the game instance.. 
} game_t;

-

game_t *game = …;

game->snake.head->direction = …;

// snake_info requires a snake_t reference so pass the address of game->snake
//snake_info(game->snake);
snake_info(&(game->snake));
snake_destroy(&(game->snake));


最小指针:

typedef struct game {
    snake_t snake; // A snake_t instance is created with the game instance.. 
} game_t;

-

// Use a game_t variable instead of a pointer
//game_t *game = …;
game_t aGame = …; // renamed variable to avoid conflicts

// change the snake.head to snake->head since the game object uses a snake_t pointer
// game->snake.head->direction = …;
aGame.snake.head->direction = …; 


// Pass the address of aGame.snake 
snake_info(&(aGame.snake));

// this line is okay since you pass the address of game->snake.
snake_destroy(&(aGame.snake));


更多地使用指针:

typedef struct game {
    snake_t *snake; // A pointer to a snake_t instance. 
} game_t;

-

// Create a game_t instance
game_t aGame = …;

// Create a snake_t instance
snake_t aSnake = ... ;


// Create a game_t pointer and assign it the address of aGame
game_t *aGamePtr = &aGame;

// Using aGamePtr, assign the snake variable in aGame the address of aSnake
aGamePtr->snake = &aSnake;


// Since the game_t object is a snake_t pointer,
// change the snake.head to snake->head 
// game->snake.head->direction = …;
aGamePtr->snake->head->direction = …; 


// this line is okay since aGamePtr->snake is a pointer.
snake_info(aGamePtr->snake);

// Since game object uses a snake_t pointer, pass aGamePtr->snake directly
// snake_destroy(&(game->snake));
snake_destroy(aGamePtr->snake);