【问题标题】:How to print member data from a struct using pointer to struct in C如何在C中使用指向结构的指针从结构中打印成员数据
【发布时间】: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 pointers struct


【解决方案1】:

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

struct Map *map_ptr;    

到:

Map *map_ptr;    

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

【讨论】:

    【解决方案2】:

    你绊倒了 C 中所谓的 命名空间

    有单独的命名空间
    • typedef 名称,正如您在 typedef struct { ... } Map; 中介绍的那样
    • 结构标签,正如您在struct Map *map_ptr; 中介绍的那样
    • 加上对象、宏名称等的其他命名空间...

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

    解决问题的一种方法是摆脱 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.
    };
    struct Map *map_ptr = ...;
    

    【讨论】:

    【解决方案3】:

    这是一个完整的示例,可能有助于澄清几点:

    #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);
    

    【讨论】:

      【解决方案4】:

      答案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{...}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-06
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 2019-03-27
        • 2016-09-24
        • 2017-03-11
        • 2010-12-07
        相关资源
        最近更新 更多