Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print the structure fields and values in C

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

ex: print (T_THREAD) and output should be like

Contents of a structure T_THREAD are    thread_id   is_valid 
like image 771
user3555115 Avatar asked Dec 23 '16 16:12

user3555115


People also ask

How do you print a struct value?

It's often necessary to print out struct values to the console for the purpose of debugging. If you use the go-to fmt. Println method, it only prints the values of the struct without the field names. Another option is to use json.

How do I print a string in a struct?

Instead, you should pass the address of the first character of the string which is: scanf("%s %d", &(tempname[0]), &temptel); or more simply: scanf("%s %d", tempname, &temptel);

What is structure structure in C?

Syntax to Define a Structure in CstructName: This is the name of the structure which is specified after the keyword struct. data_Type: The data type indicates the type of the data members of the structure. A structure can have data members of different data types.

What is structure in C with example?

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, float, char, etc.).


1 Answers

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.

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

like image 168
Dellowar Avatar answered Sep 24 '22 13:09

Dellowar