Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
tintin++ ogg sound player script for linux
Author: Robert Smith
Submitted by: Vladaar
6Dragons ogg Soundpack
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.4
Author: Vladaar
Submitted by: Vladaar
LoP 1.46
Author: Remcon
Submitted by: Remcon
LOP 1.45
Author: Remcon
Submitted by: Remcon
Users Online
CommonCrawl, Yandex, Sogou, Majestic-12

Members: 0
Guests: 3
Stats
Files
Topics
Posts
Members
Newest Member
481
3,734
19,366
618
Micheal64X
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,643
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 >>