【发布时间】:2015-09-21 11:12:49
【问题描述】:
我有一个指向外部头文件中定义的 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);
【问题讨论】:
-
怎么不行?它什么都不做吗?它会崩溃吗?会发生什么?
-
C nit:
%pargs 必须(强制转换为)ptr-to-void 否则您会调用未定义的行为。 -
另见此链接,或下面的示例代码:publib.boulder.ibm.com/infocenter/comphelp/v8v101/…