c question: *printf'ing arrays

Rick C. Petty rick-freebsd2008 at kiwi-computer.com
Tue Jun 30 21:02:39 UTC 2009


On Tue, Jun 30, 2009 at 08:21:03PM +0200, Alexander Best wrote:
> thanks. now the output gets redirected using >. i'm quite new to programming
> under unix. sorry for the inconvenience.

No problem; we all had to learn sometime.  But what I suggested should
work for every platform that adheres to POSIX.  If you were using
fprintf/fwrite, then it would work on anything that's standard C.  As for
redirection, windows command line allows the same type of redirection.

> so i guess there is no really easy way to output an inhomogeneous struct to
> stdout without using a loop to output each array contained in the struct.

That's not something C would ever provide easily.  You may want to use a
different high-level language.  However, I often use macros for printing
pieces of structures, for example I used this to print out sizes of kernel
structures:

#define SIZE(astruct, member) \
	printf("%d\t\t.%s\n", sizeof(astruct.member), #member)

#include <sys/ktrace.h>
...
	struct ktr_header header;
	struct ktr_genio genio;

	printf("%d\tktr_header:\n", sizeof(header));
	SIZE(header, ktr_len);
	SIZE(header, ktr_type);
	SIZE(header, ktr_pid);
	SIZE(header, ktr_comm);
	SIZE(header, ktr_time);
	SIZE(header, ktr_time.tv_sec);
	SIZE(header, ktr_time.tv_sec);
	SIZE(header, ktr_tid);

	printf("\n%d\tktr_genio:\n", sizeof(genio));
	SIZE(genio, ktr_fd);
	SIZE(genio, ktr_rw);

In your case, you could make a macro for each type.  Without an example of
how you want the output to look, it's hard for us to show you code that
will produce such output.

-- Rick C. Petty


More information about the freebsd-hackers mailing list