我有一个指向外部头文件中定义的Map类型结构的指针:
typedef struct {
char *squares; //!< A pointer to a block of memory to hold the map.
int width; //!< The width of the map pointed to by squares.
int height; //!< The height of the map pointed to by squares.
} Map;
指针初始化如下:
struct Map *map_ptr;
map_ptr = create_map(*w_ptr, *h_ptr);
// create_map returns Map*, w_ptr and h_ptr are pointers to height and width fields for a map/maze.
如何打印存储在create_map中创建的Map结构中的宽度和高度值? create_map保存在外部文件中,它传递回main的唯一变量是指向地图的指针。
以下编译时出错(“错误:解除指向不完整类型的指针”)
printf("Height = %d\n", map_ptr->height);
据我所知,指针有效,因为下面的代码打印了一个内存地址:
printf("Pointer address for map = %p\n", map_ptr);
答案 0 :(得分:4)
只需删除struct
关键字:
struct Map *map_ptr;
为:
Map *map_ptr;
您已声明了无名结构,并将其定义为Map
。因此,当您声明struct Map *map_ptr;
时,编译器认为这是另一个名为Map
的结构。
答案 1 :(得分:2)
你在C中绊倒了所谓的名称空间。
有单独的名称空间typedef struct { ... } Map;
struct Map *map_ptr;
可以在不同的命名空间中重用相同的标识符。我建议永远不要打扰结构的typedef 。它只隐藏有用的信息,所有这些都可以帮助您不时地编写struct
。如果某个东西是结构或指向结构的指针,那么我想知道它,所以我知道是否使用->
或.
来访问成员。使用typedef会隐藏有用信息,从而使其失败。
解决问题的一种方法是摆脱typedef,只使用带
的struct标签struct Map {
char *squares; //!< A pointer to a block of memory to hold the map.
int width; //!< The width of the map pointed to by squares.
int height; //!< The height of the map pointed to by squares.
};
struct Map *map_ptr = ...;
答案 2 :(得分:0)
以下是一个完整的示例,可能有助于澄清几点:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct {
char *squares; //!< A pointer to a block of memory to hold the map.
int width; //!< The width of the map pointed to by squares.
int height; //!< The height of the map pointed to by squares.
} Map;
Map *
create_map ()
{
printf ("Allocating %d bytes for map_ptr, and %d bytes for map data...\n",
sizeof (Map), 100);
Map *tmp = (Map *)malloc(sizeof (Map));
tmp->squares = (char *)malloc (100);
strcpy (tmp->squares, "Map data...");
tmp->width = 50;
tmp->height = 100;
return tmp;
}
int
main(int argc, char *argv[])
{
Map *map_ptr = create_map();
printf ("map_ptr->height= %d, width=%d, squares=%s\n",
map_ptr->height, map_ptr->width, map_ptr->squares);
free (map_ptr->squares);
free (map_ptr);
return 0;
}
示例输出:
Allocating 12 bytes for map_ptr, and 100 bytes for map data...
map_ptr->height= 100, width=50, squares=Map data...
另一种方法是使用“struct Map {...}”而不是typedef:
实施例
struct Map {
char *squares; //!< A pointer to a block of memory to hold the map.
int width; //!< The width of the map pointed to by squares.
int height; //!< The height of the map pointed to by squares.
} Map;
struct Map *
create_map ()
{
...
struct Map *tmp = (struct Map *)malloc(sizeof (struct Map));
...
}
...
struct Map *map_ptr = create_map();
printf ("map_ptr->height= %d, width=%d, squares=%s\n",
map_ptr->height, map_ptr->width, map_ptr->squares);
free (map_ptr->squares);
free (map_ptr);
答案 3 :(得分:0)
答案1:
struct Map *map_ptr;
到
Map *map_ptr;
答案2:
typedef struct {
char *squares; //!< A pointer to a block of memory to hold the map.
int width; //!< The width of the map pointed to by squares.
int height; //!< The height of the map pointed to by squares.
} Map;
到
struct Map{
char *squares; //!< A pointer to a block of memory to hold the map.
int width; //!< The width of the map pointed to by squares.
int height; //!< The height of the map pointed to by squares.
} ;
原因:
if typedef struct{...} B;
所以
B == struct B{...}