Suppose I have structure like:
typedef struct {
int a;
int b;
} my_struct;
Is there any way to get/print the name of the fields of that structure (i.e. "a" and "b")
programmatically?
Suppose I have structure like:
typedef struct {
int a;
int b;
} my_struct;
Is there any way to get/print the name of the fields of that structure (i.e. "a" and "b")
programmatically?
asked whether a C program can print a struct's field names at runtime. is correct that plain C does not provide runtime reflection or preserve source identifiers in a release binary, and 's simple string-array workaround is the common practical answer. Below are maintainable alternatives that avoid manual duplication and give safe metadata you can use at runtime.
A compact, compile-time pattern is the X‑macro (central list expanded in two ways). List each field once, then expand that list to define the struct and to build a metadata table (name, offset, size, type-tag if needed). This keeps names and types in sync and gives you offsets via offsetof() so you can compute addresses or pretty-print fields without brittle hand‑maintenance.
#include <stdio.h>
#include <stddef.h>
#define MY_FIELDS \
X(a, int) \
X(b, int)
#define X(name, type) type name;
typedef struct { MY_FIELDS } my_struct;
#undef X
struct field_info { const char *name; size_t offset; size_t size; };
#define X(name, type) { #name, offsetof(my_struct, name), sizeof(((my_struct*)0)->name) },
struct field_info my_struct_fields[] = { MY_FIELDS };
#undef X
/* iterate my_struct_fields[] to print names/offsets/sizes */ Notes and cautions: offsets and order reflect the compiler’s layout (padding can occur), so use offsetof rather than assuming contiguous packing. If you need to inspect actual values at runtime, you must also record type information (or a discriminator) so you can cast and read correctly. For large codebases, prefer generating the metadata at build time (a small script using libclang, pycparser, or a custom parser) rather than fragile text‑search hacks. Reading DWARF/debug info can extract names from a binary built with -g, but that’s heavyweight and not suitable for production.
Recommendation: for most needs, use X‑macros or a build‑time generator to produce a single source of truth that emits both the struct and its name/offset metadata. This is robust, portable, and keeps ’s idea DRY.
Jump to Post— ArkM 1,090No any member (variables, functionc etc) names at run time. Your running (compiled, linked and loaded) program is a block of machine commands and constants - an easy and healthy food for processor(s)...
No any member (variables, functionc etc) names at run time. Your running (compiled, linked and loaded) program is a block of machine commands and constants - an easy and healthy food for processor(s)...
Not without storing the names as strings somewhere:
#include <stdio.h>
typedef struct {
int a;
int b;
} my_struct;
const char * const my_struct_fields[] = {
"a", "b"
};
int main()
{
size_t n = sizeof my_struct_fields / sizeof my_struct_fields[0];
size_t i;
for (i = 0; i < n; ++i)
printf("Field [%d]: %s\n", i, my_struct_fields[i]);
return 0;
} We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.