C语言 在 C 中打印结构字段和值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41304497/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:32:44  来源:igfitidea点击:

Print the structure fields and values in C

cprintfstructure

提问by user3555115

I am interested in printing the structure fields .

我对打印结构字段感兴趣。

Typedef struct
{
   UINT32 thread_id;
   BOOL   is_valid;
}T_THREAD;

Is there a way in "C" language to print the contents of a structure, something like

“C”语言有没有办法打印结构的内容,比如

ex: print (T_THREAD)and output should be like

例如:print (T_THREAD)和输出应该像

Contents of a structure T_THREAD are 
  thread_id
  is_valid

回答by Sanchke Dellowar

What you're looking for is reflection. Java and other virtual languages has reflection so you can print out the variable names and function names for any given class. Because the compiler builds these reflection functions automatically.

你要找的是反射。Java 和其他虚拟语言具有反射功能,因此您可以打印出任何给定类的变量名和函数名。因为编译器会自动构建这些反射函数。

C does not have reflection. You must do everything manually.

C 没有反射。您必须手动完成所有操作。

回答by Hawk

As for your structure, the function would look something like this..

至于你的结构,函数看起来像这样..

// st_name is the name of the struct
void print(T_THREAD *st, const char *st_name)
{
    printf("Contents of structure %s are %lu, %d\n", st_name, st->thread_id, st->is_valid);
}

回答by Sourav Ghosh

No,there is no standard/ pre-definedway of achieving what you want.

不,没有标准/预定义的方式来实现你想要的。

Yes, however, you may write your own function, which has knowledge of the particular structure element valuesto be printed, and then, with a single call to the function with a particular instance of the structure variable, you can get all the values of all the members get printed. Something like

是的,但是,您可以编写自己的函数,该函数知道要打印的特定结构元素值,然后,通过使用结构变量的特定实例对该函数进行一次调用,您可以获得所有成员都被打印出来。就像是

T_THREAD var;

my_print(var);  //my_print() is the function you'll roll out

should be able to print the values.

应该能够打印值。

However, please note, this does notallow you to print the variable namesin any ways, only "values".

但是,请注意,这不会允许您打印变量名称的任何方式,只有“值”。

回答by RoadRunner - MSFT

There is no way you can print all the structure elements with a single printfstatement. In C you have to print them all out manually. Here is an example of creating two structure members and printing them out:

您无法使用单个printf语句打印所有结构元素。在 C 中,您必须手动将它们全部打印出来。这是创建两个结构成员并将它们打印出来的示例:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct {
    char *name;
    int thread_id;
    bool is_valid;
}T_THREAD;

int
main(void) {
    T_THREAD T1 = {"T1", 123, 1};
    T_THREAD T2 = {"T2", 456, 0};

    printf("\nContents of a structure %s are:\n", T1.name);
    printf("thread_id: %d\n",T1.thread_id);
    printf("is_valid: %d\n", T1.is_valid);

    printf("\nContents of a structure %s are:\n", T2.name);
    printf("thread_id: %d\n",T2.thread_id);
    printf("is_valid: %d\n", T2.is_valid);

    return 0;
}

Output:

输出:

Contents of a structure T1 are:
thread_id: 123
is_valid: 1

Contents of a structure T2 are:
thread_id: 456
is_valid: 0

Alternatively, you can create a function to do this also:

或者,您也可以创建一个函数来执行此操作:

int
main(void) {
    T_THREAD T1 = {"T1", 123, 1};
    T_THREAD T2 = {"T2", 456, 0};

    print_struct_elements(&T1);
    print_struct_elements(&T2);

    return 0;
}

void
print_struct_elements(T_THREAD *T) {
    printf("\nContents of a structure %s are:\n", T->name);
    printf("thread_id: %d\n",T->thread_id);
    printf("is_valid: %d\n", T->is_valid);
}

回答by lordhog

One way of doing this would be using x-macros. Though not as pretty compared to reflection that is built into a language. Using macro does make the code a little harder to read, but it is pretty straight forward.

这样做的一种方法是使用 x-宏。虽然与内置于语言中的反射相比并不漂亮。使用宏确实会使代码更难阅读,但它非常简单。

#include <stdio.h>
#include <stdint.h>

#define HDR_STRUCT_FIELDS       \
  FLD(0, Field_1, uint32_t, 3)  \
  FLD(1, Field_2, uint16_t, 4)  \
  FLD(2, Field_3, uint8_t,  5)

#define FLD(idx, fld, dt, initVal)   dt fld;
typedef struct 
{
    HDR_STRUCT_FIELDS
} HdrData_t;
#undef FLD

#define FLD(idx, fld, dt, initVal)   .fld = initVal,
HdrData_t HeaderData = 
{
   HDR_STRUCT_FIELDS
};
#undef FLD


#define QUOTE(field)     #field
#define FLD(idx, fld, dt, initVal)   [idx] = QUOTE(fld),
const char* HeaderDataName[] = 
{
   HDR_STRUCT_FIELDS
};
#undef FLD


int main()
{   
    #define FLD(idx, fld, dt, initVal)   printf("%s = %d\n", HeaderDataName[idx], HeaderData.fld);
    HDR_STRUCT_FIELDS
    #undef FLD

    return (0);
}

Another method where the ordering of the field is not as important would be the following which uses a structure and initializing the structure.

字段排序不那么重要的另一种方法是使用结构并初始化结构的以下方法。

#include <stdio.h>
#include <stdint.h>

#define HDR_STRUCT_FIELDS    \
  FLD(Field_1, uint32_t, 3)  \
  FLD(Field_2, uint16_t, 4)  \
  FLD(Field_3, uint8_t,  5)

#define FLD(fld, dt, initVal)   dt fld;
typedef struct
{
    HDR_STRUCT_FIELDS
} HdrData_t;
#undef FLD


#define FLD(fld, dt, initVal)   .fld = initVal,
HdrData_t HeaderData =
{
   HDR_STRUCT_FIELDS
};
#undef FLD


#define FLD(fld, dt, initVal)   const char *fld;
typedef struct
{
   HDR_STRUCT_FIELDS
} HdrDataFldString_t;
#undef FLD

#define STRINGIZE(field)        #field
#define FLD(fld, dt, initVal)   .fld = STRINGIZE(fld),
HdrDataFldString_t  HdrDataFldString =
{
   HDR_STRUCT_FIELDS
};
#undef FLD


int main()
{
    #define FLD(fld, dt, initVal)   printf("%s = %d\n", HdrDataFldString.fld, HeaderData.fld);
    HDR_STRUCT_FIELDS
    #undef FLD

    return (0);
}