By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,397 Members | 2,515 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,397 IT Pros & Developers. It's quick & easy.

print struct automaticly

P: n/a
I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly

struct A

{
int i;
char * s;
}

A a;

it will print it's contents :
i=0
s="somestring
etc;
Thanx.
Kat.

Oct 9 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
ka*****@gmail.com wrote:
I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly

struct A

{
int i;
char * s;
}

A a;

it will print it's contents :
i=0
s="somestring
No. There is no such thing in ISO C, and if you want to write one
yourself you'll have a nice task on your hands, too. For starters, there
is no way in ISO C to let your program get at the names (rather than the
values) of a random struct member - except for remembering it from the
start, which rather puts a dent in the generality of the tool.

Richard
Oct 9 '06 #2

P: n/a
Richard Bos wrote:
ka*****@gmail.com wrote:
>I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly

struct A

{
int i;
char * s;
}

A a;

it will print it's contents :
i=0
s="somestring

No. There is no such thing in ISO C, and if you want to write one
yourself you'll have a nice task on your hands, too. For starters, there
is no way in ISO C to let your program get at the names (rather than the
values) of a random struct member - except for remembering it from the
start, which rather puts a dent in the generality of the tool.
A sufficiently cunning tool -- or sufficiently transparent markup in
the C code -- can generate the printing code from the struct declaration,
yes? General enough?

Of course there are all sorts of issues about /how/ the items in the
struct(s) should be printed. Generally speaking, I'd write the
struct-printer function as part of the cluster of functions that
belong with the struct [1]; since large structs from smaller structs
grow, I wouldn't be faced with the "oh /frelling/ tedium, I have
to write a print function for a 117-element struct /now/!". (Also
I wouldn't write a flat 117-element struct anyway.)

[1] EG print one, initialise one, malloc one, free (a pointer to) one,
test two of them for equality/similarity, construct one from a
simple string description, send one to bed with a cup of cocoa
and a good book.

--
Chris "this week's vote: /The Riddle-Master of Hed/" Dollin
"It took a very long time, much longer than the most generous estimates."
- James White, /Sector General/

Oct 9 '06 #3

P: n/a

ka*****@gmail.com wrote:
I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly
You may like to look at some platform-specifics (like the STABS and
DWARF debugging formats) to see if they are appropriate, but that's not
part of the standard C language.

For general purposes, Chris Dollin's suggestion seems most appropriate.

Oct 9 '06 #4

P: n/a


On Oct 9, 10:09 pm, katy...@gmail.com wrote:
I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly

struct A

{
int i;
char * s;

}A a;

it will print it's contents :
i=0
s="somestring
etc;
Thanx.
Kat.
Try C++ instead.

Oct 9 '06 #5

P: n/a
Chris Dollin <ch**********@hp.comwrote:
Richard Bos wrote:
ka*****@gmail.com wrote:
I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly

struct A

{
int i;
char * s;
}

A a;

it will print it's contents :
i=0
s="somestring
No. There is no such thing in ISO C, and if you want to write one
yourself you'll have a nice task on your hands, too. For starters, there
is no way in ISO C to let your program get at the names (rather than the
values) of a random struct member - except for remembering it from the
start, which rather puts a dent in the generality of the tool.

A sufficiently cunning tool -- or sufficiently transparent markup in
the C code -- can generate the printing code from the struct declaration,
yes? General enough?
Yes, but it can't be part of the program itself. You can certainly write
a tool that reads C code, and outputs more C code that prints the
contents of a struct defined in the first bit of code. You can also link
the second bit of code into the first bit, and call it from there. But
that's a two-stage process at compile-time.
What you can't (portably, or even sanely) do is link the tool into the
first bit of code, and make it a one-stage process, generating the
printing code on the fly, at run-time.
Of course there are all sorts of issues about /how/ the items in the
struct(s) should be printed. Generally speaking, I'd write the
struct-printer function as part of the cluster of functions that
belong with the struct [1];
There is that; and in a normal program, you wouldn't usually want a
simple dump like this in the first place. In fact, you might well want
more than one, for different parts of the program.

Richard
Oct 9 '06 #6

P: n/a
Cong Wang wrote:
>
On Oct 9, 10:09 pm, katy...@gmail.com wrote:
>>I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly

struct A

{
int i;
char * s;

}A a;

it will print it's contents :
i=0
s="somestring
etc;
Thanx.
Kat.


Try C++ instead.
Just to satisfy my curiosity:
How would you do that in C++?

jacob
Oct 9 '06 #7

P: n/a

ka*****@gmail.com wrote:
I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly
not built into C. Y

You could write a 33-line Perl program that might do a passable job for
simple structs.

And IIRC old IBM EXTENDED FORTRAN IV (1966) had a NAMELIST feature
that pretty much does exactly that. And not only for output, you could
use it for INPUT.

But we digress.

Oct 9 '06 #8

P: n/a
On Mon, 2006-10-09 at 17:23 +0200, jacob navia wrote:
Cong Wang wrote:

On Oct 9, 10:09 pm, katy...@gmail.com wrote:
>I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly

struct A

{
int i;
char * s;

}A a;

it will print it's contents :
i=0
s="somestring
etc;
Thanx.
Kat.

Try C++ instead.

Just to satisfy my curiosity:
How would you do that in C++?
I believe that you would make a class for your object, and make it a
friend of the iostream object (so that it would work with cout/cin).

I can't for the life of me remember how to do that, though.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 9 '06 #9

P: n/a
Andrew Poelstra wrote:
On Mon, 2006-10-09 at 17:23 +0200, jacob navia wrote:
>>Cong Wang wrote:
>>>On Oct 9, 10:09 pm, katy...@gmail.com wrote:
I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly

struct A

{
int i;
char * s;

}A a;

it will print it's contents :
i=0
s="somestring
etc;
Thanx.
Kat.
Try C++ instead.

Just to satisfy my curiosity:
How would you do that in C++?


I believe that you would make a class for your object, and make it a
friend of the iostream object (so that it would work with cout/cin).

I can't for the life of me remember how to do that, though.
Anyway in C++ structs are classes...

The iostream classes will automatically figure out how to print each
member of your structure?

That would be real news to me.
Oct 9 '06 #10

P: n/a
Andrew Poelstra <ap*******@false.sitewrote:
I believe that you would make a class for your object, and make it a
friend of the iostream object (so that it would work with cout/cin).
I don't believe that's what OP wants; I think OP is looking for
something much like the reflective/introspective capabilities of Java,
which of course are no more available in C++ than they are in C.
I can't for the life of me remember how to do that, though.
It's in the C++ FAQ, if you're so inclined.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Oct 9 '06 #11

P: n/a
ka*****@gmail.com writes:
I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly

struct A

{
int i;
char * s;
}

A a;

it will print it's contents :
i=0
s="somestring
etc;
The easiest approach is probably going to be to write an output
routine for each of your structs. The drawback, of course, is that it
becomes harder in proportion to how many structs you have, and how
many members each one has.

There's nothing built into the language that will do this for you.

If you're *really* ambitious, you can write a program that parses your
C source, finds structure declarations, and generates C source for
routines to print them. This is a lot of work, but once it's done
it's no more difficult to add more structs.

I've done something similar in the past (I don't have the code).
Parsing C is difficult; typedefs make it a very complicated job.

Another approach would be to use a data definition language of some
sort, and generate both your struct definitions *and* the printing
routines from that. This saves you the effort of building a full C
parser, but it means that some of your declarations will be
automatically generated, which can have drawbacks.

There may be existing tools to do some of this.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 9 '06 #12

P: n/a

<ka*****@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly
Sorry, no. And, it definately isn't a task for someone new to C. This was
considered to be an expert task in PL/1 and it is much harder to completely
implement in C. If you're still interested, I've provided more information.

It is possible to partially automate the process. I'd recommend manually
feeding the just the code for the C structs into additional programs, the
output of which could be used by your main program to display the data.

The key to doing this easily, instead of completely, is that your C
structures would need to be free of typedef's: no typedef'd structs or
typedef'd data types. If your structs have data which use typedef's, the
task will be very difficult since you need to "undo" or "unroll" the
typedef's which could be anywhere within a "mountain" of code. It would
require almost a full C parser and preprocessor instead of a simple grammar
or text processing.

I previously posted this here (I used flex but might be able to use AWK for
typedef free structs...):

"Your question reminds me of a record editor for a database. I programmed
one of those a number of years ago in PL/1. I ended up using a number of
tools to do the job. The final output had a single line for each piece of
data in the structure which contained the name of the data, type of data,
offset from the start of the structure, etc. I used a flex grammar to parse
the PL/1 data structure into a clean format. I then used PL/1 program which
read the clean format and structure definition to output the size and
offsets of the data. This created a generic "map" for the record. This
generic map could then be loaded into the record editing program.

You should be able to do something similar in C with the help of a simple
grammar. I would start by looking sizeof() and offsetof()."

Come to think of it, I think I also output the fully qualified name. For
PL/1, I think I was using the size for the length of strings which are
fundamental type in that language. Anyway, you said you needed to transform
this:
struct A

{
int i;
char * s;
}

A a;
For C you'd probably want something with a field layout like this:

fully qualified name, some indicator of variable type, offset of
element, sizeof element (maybe unneeded)

And, the transformed or output data for each field filled in for your struct
would look something like this:

a.i, int, 0, 4
a.s, char *, 4, 4

You'd then need a routine which reads in the layout and another which a)
prints the name, b) uses the type information to select an appropriate
intermediate variable, c) casts the variable to the current structure
address + offset, and d) then displays the variable.

C also has variable arguments functionality with varargs
(va_list,va_start,va_arg,va_end). If you can figure out how to use these
for this situation, they might simplify your implementation. If you want to
see how varargs are used, some *printf() routines use them.
Although it is slightly different from what you want, this post by David
Tribble might give you some ideas as how to progress:
http://groups.google.com/group/comp....5dbe23fc?hl=en

(e.g., He uses static or hard coded names instead of reading in the names
from a layout...)
Rod Pemberton
Oct 10 '06 #13

P: n/a
Richard Bos wrote:
Chris Dollin <ch**********@hp.comwrote:
>A sufficiently cunning tool -- or sufficiently transparent markup in
the C code -- can generate the printing code from the struct declaration,
yes? General enough?

Yes, but it can't be part of the program itself.
Concur.
You can certainly write
a tool that reads C code, and outputs more C code that prints the
contents of a struct defined in the first bit of code. You can also link
the second bit of code into the first bit, and call it from there. But
that's a two-stage process at compile-time.
Yes.
What you can't (portably, or even sanely) do is link the tool into the
first bit of code, and make it a one-stage process, generating the
printing code on the fly, at run-time.
Indeed.

(Oh, agreement, it's so /boring/!)
>Of course there are all sorts of issues about /how/ the items in the
struct(s) should be printed. Generally speaking, I'd write the
struct-printer function as part of the cluster of functions that
belong with the struct [1];

There is that; and in a normal program, you wouldn't usually want a
simple dump like this in the first place. In fact, you might well want
more than one, for different parts of the program.
I've found that it's useful to have at least two lying around: the
one that prints the Whole Thing, warts and all, and the one that
prints a Pretty View. (Of course that might end up being a difference
in a prettiness parameter.)

Oh /bother/, I agreed again. Must take less sleep.

--
Chris "resembling an iron" Dollin
Nit-picking is best done among friends.

Oct 10 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.