Reading and Printing Struc Contents

Posted by Rash on Tue 05 Jan 2010 07:55 PM — 6 posts, 21,504 views.

United Kingdom #0
I'm having problems trying to list the contents of a struc to the player. I currently have this:


if ( !str_cmp( arg1, "list" ) )
{
     for ( i = 0; i < MAX_SMITH_ITEMS; i++ )
     {
	  pager_printf( ch, "Item: %s, Increment: %d\n\r", smith_items_table->name, i );
     }
     return;
}


Fist few lines of the struc;


const   struct  smith_items_type smith_items_table [] =
{
	{   0, "NULL",          0, 0, 0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "NULL"      }, /* 0 */
	{   1, "knife",          10, 10, 10,  5,  ITEM_TAKE|ITEM_WIELD, ITEM_WEAPON, 0, 1, 4, 0, 0, 0, 0, 0, "pierce"      }, /* 1 */
	{   2, "cock-spur",      5,  5,  10,  4,  ITEM_TAKE|ITEM_WIELD, ITEM_WEAPON, 0, 1, 3, 0, 0, 0, 0, 0, "pierce"  }, /* 2 */
	{   3, "stiletto",       12, 10, 15,  5,  ITEM_TAKE|ITEM_WIELD, ITEM_WEAPON, 0, 1, 5, 0, 0, 0, 0, 0, "stab"	      }, /* 3 */
	{   4, "dagger",         15, 10, 20,  30,  ITEM_TAKE|ITEM_WIELD, ITEM_WEAPON, 12, 1, 6, 2, 0, 0, 0, 0, "pierce"	      }, /* 4 */


And this is the output I get when I do 'forge list'


Item: NULL, Increment: 0
Item: NULL, Increment: 1
Item: NULL, Increment: 2
Item: NULL, Increment: 3
Item: NULL, Increment: 4
Item: NULL, Increment: 5
Item: NULL, Increment: 6
Item: NULL, Increment: 7
Item: NULL, Increment: 8
Item: NULL, Increment: 9
Item: NULL, Increment: 10
Item: NULL, Increment: 11
Item: NULL, Increment: 12
Item: NULL, Increment: 13
Item: NULL, Increment: 14
Item: NULL, Increment: 15


I think I see the problem but not sure how to solve it. It's not actually checking the each row of the struc. Its only checking the first line. I did original have;


pager_printf( ch, "Item: %s, Increment: %d\n\r", smith_items_table[i].name, i );


That just crashes the mud upon 'forge list'.

Where am I going wrong? Am I simply trying to access the data incorrectly? Any help would be very much appreciated.
Amended on Tue 05 Jan 2010 09:20 PM by Nick Gammon
Australia Forum Administrator #1
Can you show the smith_items_type definition please?

I think your original code was closer, you have to index into the table, or everything will be the first item. I can't see offhand why that crashed.
United Kingdom #2
Here they are Nick;


extern const struct smith_items_type smith_items_table[];

/* structs setup */
struct smith_items_type
{
    int	  index;
	char *name;
	int   quantity;
	int   weight;
	int   difficulty;
	int   beats;
	int   wear_flags;
	int   item_type;
	int   base_v0;
	int   base_v1;
	int   base_v2;
	int   base_v3;
	int   base_pierce;
	int   base_bash;
	int   base_slash;
	int   base_exotic;
	char *dam_noun;
};
USA #3
The original method makes more sense (iterating over the structs in the array of structs):


pager_printf( ch, "Item: %s, Increment: %d\n\r", smith_items_table[i].name, i );


Your "almost good" output shows that the array exists, you're dereferencing it with -> (same thing as smith_items_table[0]) and accessing the name element ("NULL"). Since i isn't involved, you get only "NULL" printed out.

I assume that MAX_SMITH_ITEMS is 16, because your loop tries to print out 16 items. Make sure your array of structs has 16 elements (with valid strings in the name position).
Amended on Wed 06 Jan 2010 02:56 AM by Nick Gammon
Australia Forum Administrator #4
Good point about the number of items. Your code is a bit dangerous because the table has an undefined length but you are going through it MAX_SMITH_ITEMS times. How do you know the table is the correct size?

Instead of MAX_SMITH_ITEMS you could use NUMITEMS:


#define NUMITEMS(x) (sizeof(x) / sizeof(x[0]))


In this case, it would be:


for ( i = 0; i < NUMITEMS (smith_items_table); i++ )
 // blah blah


Now it iterates through for exactly the number of entries you put into it.
United Kingdom #5
I've defined its max size previous, I actually forgot to show that piece of code, as shown below:


#define MAX_SMITH_ITEMS  100


As you can see its defined as 100 max items (it does go through all 100 items, the output was cut from the example above to save on room).

I double checked the size of the struc as I had a naggling doubt and as yo both pointed it it may have smething to do with its size, low and behold it was missing 6 entries. I corrected that and nothing changed. So I've tried Nicks suggestion of:


for ( i = 0; i < NUMITEMS (smith_items_table); i++ )


When my code to use the above like so:


/* Not yet working, will list alll available items. */

	if ( !str_cmp( arg1, "list" ) )
	{
		for ( i = 0; i < NUMITEMS (smith_items_table); i++ )
		{
		  pager_printf( ch, "Item: %s, Increment: %d\n\r", smith_items_table .name, i );
		}
		return;
	}


It now works perfectly!


Item: NULL, Increment: 0
Item: knife, Increment: 1
Item: cock-spur, Increment: 2
Item: stiletto, Increment: 3
Item: dagger, Increment: 4
Item: keris, Increment: 5
Item: dirk, Increment: 6
Item: poinard, Increment: 7
Item: long-knife, Increment: 8
Item: shortsword, Increment: 9
Item: langsax, Increment: 10
Item: leaf-sword, Increment: 11
Item: gladius, Increment: 12
Item: cutlass, Increment: 13
Item: sabre, Increment: 14
Item: rapier, Increment: 15
Item: cinqueda, Increment: 16
Item: spatha, Increment: 17
Item: falchion, Increment: 18
Item: scimitar, Increment: 19
Item: nimcha, Increment: 20
Item: hanger, Increment: 21
Item: kastane, Increment: 22


Thank you very much guys. Nick that little bit of code worked great. Will have to use that in the future if I have problems outputting strucs like that.
Amended on Wed 06 Jan 2010 10:35 PM by Rash