c question: *printf'ing arrays

Carlos A. M. dos Santos unixmania at gmail.com
Thu Jul 2 01:48:25 UTC 2009


On Tue, Jun 30, 2009 at 7:06 PM, Alexander
Best<alexbestms at math.uni-muenster.de> 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]);

The code below is a bit more efficient and easier to read. It is also
safer, since it limits the width of the %s conversions to the sizes of
the corresponding arrays.

	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"
		"\nGame Code: %.*s"
		"\nMaker Code: %.*s"
		"\nFixed Value: 0x%x"
		"\nUnit Code: 0x%x"
		"\nDevice Type: 0x%x"
		"\nReserved Area: 0x",
      		sizeof(hdr->game_title), hdr->game_title,
		sizeof(hdr->game_code), hdr->game_code,
		sizeof(hdr->maker_code), hdr->maker_code,
		hdr->fixed_val,
		hdr->unit_code,
		hdr->device_type);
	for (i=0; i < sizeof(hdr->reserved_area1); i++)
		fprintf(stderr, "%x", hdr->reserved_area1[i]);
	fprintf(stderr,
		"\nSoftware Version: 0x%x"
		"\nComplement Check: 0x%x"
		"\nReserved Area: 0x%x"
		"\nRAM Entry Point: 0x",
		hdr->software_version,
		hdr->complement_check,
		hdr->reserved_area2);
	for (i=0; i < sizeof(hdr->ram_entry); i++)
		fprintf(stderr, "%x", hdr->ram_entry[i]);
	fprintf(stderr,
		"\nBoot Mode: 0x""%x"
		"\nSlave ID: 0x""%x"
		"\nUnused Area: 0x",
		hdr->boot_mode,
		hdr->slave_id);
	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]);
	fprintf(stderr, "\n");



-- 
My preferred quotation of Robert Louis Stevenson is "You cannot
make an omelette without breaking eggs". Not because I like the
omelettes, but because I like the sound of eggs being broken.


More information about the freebsd-hackers mailing list