Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Struct?

Posted on 2005-05-04
13
Medium Priority
?
465 Views
Last Modified: 2010-04-02
struct header
{
  int name;
  int address;
}

I was wondering is it possible to print the variable name on the struct? say for example in here print the name and the address? (NOT the value) but the variable name? without looking the header files?
0
Comment
Question by:sutejok
11 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 total points
ID: 13932511
Hi sutejok,

If the program is compiled with debug options, you have a good understanding of the debug tables, and you're able to parse the tables to find the structure definition, then yes.

But to most of us "mere mortals", the answer is a resounding, "nope".


Good Luck!
Kent
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13933016
Kent is right ... In plain C, nope ... Unless you write code to read your struct declarations from your own .h files, parse and store them in a table which is indexed on the struct name.

Another alternative might be to write scripts to do that for you and invoke the scripts at run time.
Yet another way would be - as Kent said - use debug option and parse the embedded debug information.
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13933924
Or switch to Java.

Paul
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 22

Expert Comment

by:grg99
ID: 13934822
It's not quite as hard as all that...   If you ask the linker to generate a full link map, then all you global variables should be iin the map file, along with their addresses.

But only for globals, not for any locals.

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13935156
I think will be interesting to know what for. There is always a solution. Not necessary to switch to Java, could be C++ instead ;)
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13935235
Under Windoze there's the .bsc file designed for browsers. I dont know if its structure is public though, it certainly ought to be.

Paul
0
 
LVL 1

Expert Comment

by:stuartdehaan
ID: 13935695
If you mean to print the variable names from a compiled program, not in the debugger, this is not possible. The names of variables are not saved in a compiled program; variables are just blocks of memory, given a name by the programmer, but not in machine-code.

Stuart
0
 
LVL 4

Expert Comment

by:furqanchandio
ID: 13950202
hi


u could use this

struct header
{
  int name;
  int address;
char n[6];
char a[8];
} ;

strcpy( header.n,"name" );
strcpy( header.a,"address" );

that is the only i know i of


0
 
LVL 11

Expert Comment

by:cup
ID: 13955590
If you use C++ and switch on RTTI, you could use typeid eg

struct header
{
    ...
} xxx;

cout << typeid(xxx).name () << endl;

will print "header"
0
 
LVL 3

Expert Comment

by:balder
ID: 13966014
Well, it should be possible, it depends on the preconditions :)

When and how do you intend to write the names ?

To print the values you would do eg.
printf( "%d", header_ptr->name );

to print the names nothing will stop you from doing
printf( "%s", "name" );

but creating a simple parsing function that stripped of everyting up to -> would be neat, then you could do
debugprint( "header_ptr->name" );


So, the real question is, how do you intend to use this?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 14018985
That's what the preprocessor is for :)

---- START CODE BLOCK ----
#include <stdio.h>

#define PRINT_VAR_NAME(var) fprintf (stdout, "" #var)

typedef struct header {
  int name;
  int address;
} header;

int main(void) {
  header h1;
  h1.name = 1;
  h1.address = 2;
  PRINT_VAR_NAME(h1);
  fprintf(stdout, " {\n  ");
  PRINT_VAR_NAME(h1.name);
  fprintf(stdout, " = %d\n  ", h1.name);
  PRINT_VAR_NAME(h1.address);
  fprintf(stdout, " = %d\n}\n", h1.address);
  return 0;
}
---- END CODE BLOCK ----

This will print :

h1 {
  h1.name = 1
  h1.address = 2
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question