C语言 如何在C中使用指向结构的指针从结构中打印成员数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14095242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print member data from a struct using pointer to struct in C
提问by Bradford
I have a pointer to a struct of type Map defined in an external header file:
我有一个指向外部头文件中定义的 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;
The pointer is initialised as follows:
指针初始化如下:
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.
How do I go about printing the values of width and height stored within the Map structure which is created in create_map? create_map is held in an external file and the only variable it passes back to main is the pointer to the map.
如何打印存储在 create_map 中创建的 Map 结构中的宽度和高度值?create_map 保存在一个外部文件中,它传递回 main 的唯一变量是指向映射的指针。
The following gives an error when compiling ("error: dereferencing pointer to incomplete type")
编译时出现以下错误(“错误:取消引用指向不完整类型的指针”)
printf("Height = %d\n", map_ptr->height);
As far as I know, the pointer is valid as the code below prints a memory address:
据我所知,该指针是有效的,因为下面的代码打印了一个内存地址:
printf("Pointer address for map = %p\n", map_ptr);
回答by P.P
Just drop the structkeyword from:
只需struct从以下位置删除关键字:
struct Map *map_ptr;
to:
到:
Map *map_ptr;
You have declared a nameless struct and typedef'ed it to Map. So when you declare struct Map *map_ptr;, compiler thinks this is another structcalled Map.
您已经声明了一个无名结构并将其类型定义为Map. 因此,当您声明时struct Map *map_ptr;,编译器认为这是另一个名为Map.
回答by Jens
You tripped over what is called namespacesin C. There are separate namespaces for
你被C 中所谓的命名空间绊倒了。 有单独的命名空间
- typedef names, as you introduced with
typedef struct { ... } Map; - struct tags, as you introduced with
struct Map *map_ptr; - plus other namespaces for objects, macro names, ...
- typedef 名称,如您所介绍的
typedef struct { ... } Map; - struct 标签,正如您所介绍的
struct Map *map_ptr; - 加上对象的其他命名空间,宏名称,...
The same indentifier can be reused in different namespaces. I recommend to never bother with typedefs for structs. It only hides useful information, and all it does it saving you from writing structevery now and then. If something is a struct or pointer to a struct then I want to know it so I know whether to use ->or .to access the members. Using typedefs defeats this by hiding useful information.
相同的标识符可以在不同的命名空间中重复使用。我建议永远不要为 structs 使用 typedef。它只隐藏有用的信息,它所做的一切都是为了让您免于时不时地写作struct。如果某个东西是结构体或指向结构体的指针,那么我想知道它,以便我知道是使用->还是.访问成员。使用 typedef 可以通过隐藏有用的信息来解决这个问题。
One way to fix your problem is to get rid of the typedef and only use a struct tag with
解决问题的一种方法是去掉 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 = ...;
回答by paulsm4
Here is a complete example that might help clarify a few points:
这是一个完整的示例,可能有助于澄清几点:
#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;
}
EXAMPLE OUTPUT:
示例输出:
Allocating 12 bytes for map_ptr, and 100 bytes for map data...
map_ptr->height= 100, width=50, squares=Map data...
An alternative approach would be to use "struct Map {...}" instead of the typedef:
另一种方法是使用“struct Map {...}”而不是typedef:
EXAMPLE:
例子:
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);
回答by white
Answer 1:
回答 1:
struct Map *map_ptr;
to
到
Map *map_ptr;
Answer 2:
答案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;
to
到
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.
} ;
Reason :
原因 :
if typedef struct{...} B;
so
所以
B == struct B{...}

