2

我有一个指向外部头文件中定义的 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);
4

4 回答 4

4

只需从以下位置删除struct关键字:

struct Map *map_ptr;    

至:

Map *map_ptr;    

您已经声明了一个无名结构并将其类型定义为Map. 因此,当您声明时struct Map *map_ptr;,编译器认为这是另一个名为Map.

于 2012-12-30T22:30:09.603 回答
2

你绊倒了 C 中所谓的命名空间。有单独的命名空间

  • typedef 名称,正如您介绍的那样typedef struct { ... } Map;
  • struct 标签,正如您介绍的那样struct Map *map_ptr;
  • 加上对象的其他命名空间,宏名称,......

相同的标识符可以在不同的命名空间中重用。我建议永远不要为 structs 使用 typedef。它只隐藏有用的信息,它所做的一切都是为了让你不时地写作struct。如果某物是结构或指向结构的指针,那么我想知道它,以便知道是使用->还是.访问成员。使用 typedefs 通过隐藏有用的信息来解决这个问题。

解决问题的一种方法是摆脱 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 = ...;
于 2012-12-30T22:59:33.827 回答
0

答案一:</p>

struct Map *map_ptr; 

Map *map_ptr; 

答案2:</p>

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

原因:</p>

if typedef struct{...}  B; 

所以

B == struct B{...}
于 2012-12-31T03:18:43.430 回答
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);
于 2012-12-30T22:29:23.387 回答