Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
auth_update crash
Dec 23, 2017, 10:15 pm
By Remcon
check_tumble
Dec 18, 2017, 7:21 pm
By Remcon
parse description bug
Dec 15, 2017, 10:08 pm
By Remcon
Couple bugs
Dec 12, 2017, 5:42 pm
By Remcon
Bug in disarm( )
Nov 12, 2017, 6:54 pm
By GatewaySysop
LoP 1.46
Author: Remcon
Submitted by: Remcon
LOP 1.45
Author: Remcon
Submitted by: Remcon
LOP Heroes Edition
Author: Vladaar
Submitted by: Vladaar
Heroes sound extras
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.3
Author: Vladaar
Submitted by: Vladaar
Users Online
CommonCrawl

Members: 0
Guests: 16
Stats
Files
Topics
Posts
Members
Newest Member
478
3,708
19,242
612
Jacki72H
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » Codebases » AFKMud Support & Development » Arrays of Pointers
Forum Rules | Mark all | Recent Posts

Arrays of Pointers
< Newer Topic :: Older Topic > Quite unsure

Pages:<< prev 1 next >>
Post is unread #1 Aug 28, 2004, 7:51 am   Last edited Nov 24, 2007, 2:12 pm by Samson
Go to the top of the page
Go to the bottom of the page

cynshard

GroupMembers
Posts95
JoinedNov 19, 2003

You may remember me working on explode and implode functions some time ago. I used linked lists to handle the results. Greven had mentioned to me that using arrays would likely be faster. So I have started on writing the explode function again but I'm stumped on how to build and return this array.

// Returns a pointer to the first element of the array.
char *array_explode( char* delimiter, char *string )
{
	char *token;
	int size = 0, j = 0;

	if( !string || string[0] == '\0' )
  return NULL;

	// First round of tokenization
	token = strtok( string, delimiter );

	// Determine the size of the array
	do
	{
  token = strtok( NULL, delimiter );
  size++;
	} while( token != NULL );

	// Second round of tokenization
	token = strtok( string, delimiter );

	char *ret[size];

	do
	{
  ret[j] = (char*)malloc( sizeof( char* ));
  ret[j] = strtok( NULL, delimiter );
  j++;
	} while( token != NULL );

	return ret[0];
}

CMDF do_aexplode( CHAR_DATA *ch, char *argument )
{
	char * post;
	int i = 0;

	post = array_explode( "-", argument );

	while( post[i] )
  ch_printf( ch, "%s\r\n", &post[i++] );

	return;
}


The first function is the utility that breaks strings up and puts them into arrays. The second is a test function that assumes your argument is delimited by hyphens.

Thanks
Cynshard
       
Post is unread #2 Nov 17, 2004, 5:30 pm
Go to the top of the page
Go to the bottom of the page

Gwareth

GroupMembers
Posts9
JoinedNov 17, 2004

Still working on this one? Couple things struck me when looking at the code.

1. You're returning the contents of the first element in the array. You need to return &array[element] which returns the address to the first element. That is what you were asking.

2. Why would an array work better in this situation? Seems like more work to have to resize the array each time. Seems like a linked list or better yet a vector would work much better for your purposes.

- Gwareth
       
Post is unread #3 Nov 17, 2004, 6:36 pm
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

And just to ponit out, in case there are those who might wonder, with AFKMud, using a vector for something like this is in fact possible since the base compiles using g++ already. Even though none of the 1.63 release is using actual C++ constructs like that, the possibility to do so is certainly there.
       
Pages:<< prev 1 next >>