473,322 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

print struct automaticly

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
13 30894
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
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

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


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
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
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

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
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
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
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
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

<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: venkatbo | last post by:
Hi folks, Of TurboGers & Django WAF candidates, which one would be easier to use in an environment where the data/content doesn't come an RDBMS, but from other server-side apps... If these are...
2
by: Michael Yanowitz | last post by:
Hello: I am relatively new to Python and this is my first post on this mailing list. I am confused as to why I am getting size differences in the following cases: >>> print...
11
by: nephish | last post by:
hello there, all. i have a difficult app that connects to a server to get information for our database here. this server is our access point to some equipment in the field that we monitor. ...
3
by: David Bear | last post by:
I found this simple recipe for converting a dotted quad ip address to a string of a long int. struct.unpack('L',socket.inet_aton(ip)) trouble is when I use this, I get struct.error: unpack...
4
by: call_me_anything | last post by:
I have different kind of data structures in different applications. The data structures are big and complex and I would like to print the members of each struct. Can we write a generic piece of...
2
by: Heikki Toivonen | last post by:
M2Crypto has some old code that gets and sets socket timeouts in http://svn.osafoundation.org/m2crypto/trunk/M2Crypto/SSL/Connection.py, for example: def get_socket_read_timeout(self): return...
2
by: bradawk | last post by:
It has been a long while since I did any C programming. I am trying to read the utmp.h header on RHEL vs 5 but I am getting a little lost with the branching. I wanted to write a simple program that...
3
by: lye85 | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct account { char *AccName; int Age; double AccBalance; struct account *Next;
1
by: lye85 | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct account { char AccName; int Age; double AccBalance; struct account *Next;
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.