473,461 Members | 1,514 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I print this?

typedef struct { unsigned char data[MAX_DATA]; } packet;

typedef struct {
packet info;
int nro_reg;
} Buffer_Network;

void test(void)
{
FILE *filename;
Buffer_Network toRead;
filename = fopen("filename", "r");
while (fread(&toRead, sizeof(Buffer_Network), 1, filename)==1)
xxx(&toRead.info);
fclose(buffer_red);
}
void xxx(packet *p)
{
int i;
I need to print the PACKET p,
¿¿ for (i=0; i<=MAX_DATA; i++)
printf ("%c", *(p+i)); ??
¿¿ printf ("%s", p);

}
May 3 '06 #1
4 1782

Olaf El Blanco wrote:
typedef struct { unsigned char data[MAX_DATA]; } packet;

typedef struct {
packet info;
int nro_reg;
} Buffer_Network;

void test(void)
{
FILE *filename;
Buffer_Network toRead;
filename = fopen("filename", "r");
while (fread(&toRead, sizeof(Buffer_Network), 1, filename)==1)
xxx(&toRead.info);
fclose(buffer_red);
}
void xxx(packet *p)
{
int i;
I need to print the PACKET p,
¿¿ for (i=0; i<=MAX_DATA; i++)
printf ("%c", *(p+i)); ??
¿¿ printf ("%s", p);

}


Wouldn't it be more natural to have:

void xxx(Buffer_Network *p);

and within it output:

printf("%c",p->info.data[i]);

or

printf("%s",p->info.data);

provided it's zero terminated?

May 3 '06 #2
depends on what your packet contains ..

if your packet contains an ascii human readable string (null
terminated), use %s,
if your packet is a byte stream , use %c

May 3 '06 #3
On Wed, 3 May 2006 10:28:53 +0200, "Olaf \"El Blanco\""
<ve******@yahoo.no> wrote:
typedef struct { unsigned char data[MAX_DATA]; } packet;

typedef struct {
packet info;
int nro_reg;
} Buffer_Network;

void test(void)
{
FILE *filename;
Buffer_Network toRead;
filename = fopen("filename", "r");
while (fread(&toRead, sizeof(Buffer_Network), 1, filename)==1)
xxx(&toRead.info);
fclose(buffer_red);
}

Please include your question in the text of your message. Some
newsreaders do not provide visibility to the subject.
"How do I print this?"
void xxx(packet *p)
{
int i;
I need to print the PACKET p,
¿¿ for (i=0; i<=MAX_DATA; i++)
printf ("%c", *(p+i)); ??
This will print all MAX_DATA characters, without regard to how many
are actually used if the array contains a string. Is that what you
want?

printf is a pretty expensive function if you aren't taking advantage
of it's many features. putc might be more efficient. As a matter of
style, I find p[i] easier on the eyes and more intuitive than *(p+i).
¿¿ printf ("%s", p);
This will only work if the array actually contains a string.
Similarly, fputs might be more efficient.

}

Remove del for email
May 4 '06 #4
On Wed, 03 May 2006 19:00:38 -0700, Barry Schwarz <sc******@doezl.net>
wrote:
On Wed, 3 May 2006 10:28:53 +0200, "Olaf \"El Blanco\""
<ve******@yahoo.no> wrote:
typedef struct { unsigned char data[MAX_DATA]; } packet;

typedef struct {
packet info;
int nro_reg;
} Buffer_Network; Buffer_Network toRead; xxx(&toRead.info);
Note: in the OP code there is no declaration of xxx() before this
call, and the definition comes after, so in C90 it is implicitly and
wrongly declared as returning int (better compilers should give at
least a warning) and in C99 it is illegal.
Please include your question in the text of your message. Some
newsreaders do not provide visibility to the subject.
"How do I print this?"

void xxx(packet *p)
{
int i;
I need to print the PACKET p,
¿¿ for (i=0; i<=MAX_DATA; i++)
printf ("%c", *(p+i)); ??

As written this tries to access subsequent _packets_ (which don't
exist) and causes Undefined Behavior by (grossly) mismatching the
actual argument type with the format specifier for it.

Should be p->data[i] (clearest) or equivalently
*(p->data + i) or *((*p).data + i) etc.
This will print all MAX_DATA characters, without regard to how many
are actually used if the array contains a string. Is that what you
want?
Actually with the fix above it will print MAX_DATA + 1, going off the
end of the array and possibly invoking Undefined Behavior, although
arguably not in this case since it's in a struct followed by something
else that can be accessed as unsigned char(s).
printf is a pretty expensive function if you aren't taking advantage
of it's many features. putc might be more efficient. As a matter of
style, I find p[i] easier on the eyes and more intuitive than *(p+i).

Concur, see above.

To output all the characters (bytes) in the buffer as is, either of
fwrite (p->data, sizeof p->data, 1, stdout)
fwrite (p->data, 1, sizeof p->data, stdout)
or obvious/trivial variants thereof.
¿¿ printf ("%s", p);


This will only work if the array actually contains a string.
Similarly, fputs might be more efficient.

Concur.
- David.Thompson1 at worldnet.att.net
May 11 '06 #5

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

Similar topics

12
by: Michael Foord | last post by:
Here's a little oddity with 'print' being a reserved word... >>> class thing: pass >>> something = thing() >>> something.print = 3 SyntaxError: invalid syntax >>> print something.__dict__...
14
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version:...
4
by: Fred | last post by:
I'm a newbie to JavaScript so be gentle. You know that funky IE margin issue that causes the right side of the page to get cut off when the user prints the html document?...
6
by: Dennis Allen | last post by:
Hi. On one particular web page I want to offer the user a print button. Question. How do I create one? Something compatible with most browsers would be nice. Appreciate any advice...Dennis
2
by: Dean Slindee | last post by:
Anybody written code in VB.NET to: 1) show a print preview window of reports already written and stored in an Access 2002 database; or 2) execute the print of a report stored in an Access 2002...
1
by: Steff | last post by:
I am wandering if my code is making sense... I use a lot the print function. Is it weird in this case where I have to display an array ? I thought it would be better to have the entire array in php...
2
by: kbperry | last post by:
Hi all, I am getting an error message when trying to use the P4 print command via the python api for perforce. Anytime that I run p4c.run("print","-q", eachFile), I keep getting an error...
69
by: Edward K Ream | last post by:
The pros and cons of making 'print' a function in Python 3.x are well discussed at: http://mail.python.org/pipermail/python-dev/2005-September/056154.html Alas, it appears that the effect of...
2
by: Brad Pears | last post by:
I have some sample code that uses the print dialog, print preview and a print direct options. If I select print preview and then click the printer icon from that, the document prints. If I...
11
by: JWest46088 | last post by:
I'm having difficulty trying to figure out how to print a text file from a hash table one line at a time. I have the text file read into the hash table and can print the text file all at once, but I...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.