From owner-freebsd-hackers@FreeBSD.ORG Wed Jul 1 00:01:56 2009 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id BC5541065670; Wed, 1 Jul 2009 00:01:56 +0000 (UTC) (envelope-from a_best01@uni-muenster.de) Received: from zivm-out3.uni-muenster.de (ZIVM-OUT3.UNI-MUENSTER.DE [128.176.192.18]) by mx1.freebsd.org (Postfix) with ESMTP id 9D7618FC14; Wed, 1 Jul 2009 00:01:55 +0000 (UTC) (envelope-from a_best01@uni-muenster.de) X-IronPort-AV: E=Sophos;i="4.42,319,1243807200"; d="scan'208";a="7236484" Received: from zivmaildisp2.uni-muenster.de (HELO ZIVMAILUSER05.UNI-MUENSTER.DE) ([128.176.188.143]) by zivm-relay3.uni-muenster.de with ESMTP; 01 Jul 2009 02:01:54 +0200 Received: by ZIVMAILUSER05.UNI-MUENSTER.DE (Postfix, from userid 149459) id B5E821B07E4; Wed, 1 Jul 2009 02:01:54 +0200 (CEST) Date: Wed, 01 Jul 2009 02:01:54 +0200 (CEST) From: Alexander Best Sender: Organization: Westfaelische Wilhelms-Universitaet Muenster To: Alfred Perlstein Message-ID: In-Reply-To: <20090630225457.GR84786@elvis.mu.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Cc: rick-freebsd2008@kiwi-computer.com, freebsd-hackers@freebsd.org Subject: Re: c question: *printf'ing arrays X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 01 Jul 2009 00:01:57 -0000 wow. thanks. that's looking really nice. i'll change my sources tomorrow after a good dose of sleep. ;) alex Alfred Perlstein schrieb am 2009-07-01: > Hey Alex, > People frown on macros, but this could be a good one: > #define SPRINT(f, fmt) \ > do {\ > for (_i = 0; _i < sizeof(f)/sizeof(f[0]); i++) \ > printf(fmt, f[i]); \ > }while(0) > :D > This should allow you to point to any _array_ and print each > element of it using format "fmt". > Example: > SPRINT(Header->game_title, "%c"); > -Alfred > * Alexander Best [090630 15:06] > wrote: > > thanks for all the help. i decided to take the pill and coded all > > the fprintfs > > by hand. here's the result. usually i'd stick to a higher level > > languag, but i > > need C's inline assembly support: > > struct Header > > { > > u_int8_t rom_entry[4]; > > u_int8_t nintendo_logo[156]; > > u_char game_title[12]; > > u_char game_code[4]; > > u_char maker_code[2]; > > u_int8_t fixed_val; > > u_int8_t unit_code; > > u_int8_t device_type; > > u_int8_t reserved_area1[7]; > > u_int8_t software_version; > > u_int8_t complement_check; > > u_int8_t reserved_area2; > > u_int8_t ram_entry[4]; > > u_int8_t boot_mode; > > u_int8_t slave_id; > > u_int8_t unused_area[26]; > > u_int8_t joybus_entry[4]; > > }; > > struct Header * hdr = rom; > > int i; > > fprintf(stderr, "ROM Entry: 0x"); > > for (i=0; i < 4; i++) fprintf(stderr, "%x", hdr->rom_entry[i]); > > fprintf(stderr, "\nNintendo Logo: 0x"); > > for (i=0; i < sizeof(hdr->nintendo_logo); i++) fprintf(stderr, > > "%x", > > hdr->nintendo_logo[i]); > > fprintf(stderr, "\nGame Title: %s", hdr->game_title); > > fprintf(stderr, "\nGame Code: %s", hdr->game_code); > > fprintf(stderr, "\nMaker Code: %s", hdr->maker_code); > > fprintf(stderr, "\nFixed Value: 0x"); > > fprintf(stderr, "%x", hdr->fixed_val); > > fprintf(stderr, "\nUnit Code: 0x"); > > fprintf(stderr, "%x", hdr->unit_code); > > fprintf(stderr, "\nDevice Type: 0x"); > > fprintf(stderr, "%x", hdr->device_type); > > fprintf(stderr, "\nReserved Area: 0x"); > > for (i=0; i < sizeof(hdr->reserved_area1); i++) fprintf(stderr, > > "%x", > > hdr->reserved_area1[i]); > > fprintf(stderr, "\nSoftware Version: 0x"); > > fprintf(stderr, "%x", hdr->software_version); > > fprintf(stderr, "\nComplement Check: 0x"); > > fprintf(stderr, "%x", hdr->complement_check); > > fprintf(stderr, "\nReserved Area: 0x"); > > fprintf(stderr, "%x", hdr->reserved_area2); > > fprintf(stderr, "\nRAM Entry Point: 0x"); > > for (i=0; i < sizeof(hdr->ram_entry); i++) fprintf(stderr, > > "%x", > > hdr->ram_entry[i]); > > fprintf(stderr, "\nBoot Mode: 0x"); > > fprintf(stderr, "%x", hdr->boot_mode); > > fprintf(stderr, "\nSlave ID: 0x"); > > fprintf(stderr, "%x", hdr->slave_id); > > fprintf(stderr, "\nUnused Area: 0x"); > > for (i=0; i < sizeof(hdr->unused_area); i++) fprintf(stderr, > > "%x", > > hdr->unused_area[i]); > > fprintf(stderr, "\nJoybus Entry Point: 0x"); > > for (i=0; i < sizeof(hdr->joybus_entry); i++) fprintf(stderr, > > "%x", > > hdr->joybus_entry[i]); > > cheers. > > Rick C. Petty schrieb am 2009-06-30: > > > 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 > > > ... > > > 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 > > _______________________________________________ > > freebsd-hackers@freebsd.org mailing list > > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers > > To unsubscribe, send any mail to > > "freebsd-hackers-unsubscribe@freebsd.org"