Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 30 Jun 2009 16:02:38 -0500
From:      "Rick C. Petty" <rick-freebsd2008@kiwi-computer.com>
To:        Alexander Best <alexbestms@math.uni-muenster.de>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: c question: *printf'ing arrays
Message-ID:  <20090630210238.GA33849@keira.kiwi-computer.com>
In-Reply-To: <permail-20090630182103f7e55a9d0000355c-a_best01@message-id.uni-muenster.de>
References:  <20090630181121.GA32665@keira.kiwi-computer.com> <permail-20090630182103f7e55a9d0000355c-a_best01@message-id.uni-muenster.de>

next in thread | previous in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20090630210238.GA33849>