473,836 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 31038
ka*****@gmail.c om 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.c om 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="somestrin g

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.c om 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.c om 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**********@h p.comwrote:
Richard Bos wrote:
ka*****@gmail.c om 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.c om 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="somestri ng
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.c om 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.c om 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="somestrin g
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.c om 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="somestri ng
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1552
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 not good candidates, could you suggest appropriate ones... TIA, /venkat
2
2306
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 struct.calcsize("I") 4
11
1962
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. the messages come in over a socket connection. And according to their (very limited) documentation, are set up in a particular order. like this
3
4845
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 str size does not match format I thought ip addresses were unsigned 32 bit integers.
4
4989
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 code which will automatically find out the type of struct elements and print them (indented manner ?) if they are primitive types like int, float or char* ? else it will recurse into the composite data type.
2
3543
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 timeout.struct_to_timeout(self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, timeout.struct_size())) The helper timeout module is here:
2
2460
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 would create a struct of type utmp and then would print out each element name and type. Such as, "the first element is called ut_type and is an integer." I have: #include <stdio.h> # include <utmp.h> int main(int argc, char *argv) { struct...
3
3356
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
2145
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
9811
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9657
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10532
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10577
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7774
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5812
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4443
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.