Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 17th, 2005, 7:30 AM   #21
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
You should know why array[7] and 7[array]
This is germane in one sense other than valid usage. I'll tell you that I've been taken to task for using the word "value" when referring to the contents of a pointer (in the NULL pointer section), and even pointed to a site as "proof". That's horseshit, of course. Some systems consider all memory to be an array and a pointer an index into that array. No one claims that indexes aren't values. Lectric's usage, quoted above, also reveals the "valueness" of a pointer. Some people consider it poor usage. Some people don't like turnips.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 17th, 2005, 9:22 AM   #22
drifter
Programmer
 
drifter's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia
Posts: 39
Rep Power: 0 drifter is an unknown quantity at this point
Send a message via ICQ to drifter Send a message via MSN to drifter
Have to say, I was finished two classes on C++ (the second one was Data Structures) and I didn't fully understand Pointers until I was working with Linked Lists.
I do declare pointers as extremely useful!

I HAVE SPOKEN!
lol
__________________
Only two things are infinite, the universe and human stupidity, and im not sure about the former.
drifter is offline   Reply With Quote
Old Dec 17th, 2005, 10:03 AM   #23
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Your tailend contributions to recent threads are quite mind boggling :eek: .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 17th, 2005, 10:32 AM   #24
drifter
Programmer
 
drifter's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia
Posts: 39
Rep Power: 0 drifter is an unknown quantity at this point
Send a message via ICQ to drifter Send a message via MSN to drifter
LOL, yes yes, I am delayed with my posting.
I appologize for this, but I have been in school and working, so I don't have as much time to check in as I would like.
Plus I just started getting to know these languages better, so I am trying to give opinional input as well as ask questions in the form of possible answers (not related to this post in general, but others) since this is one of the ways I learn.

Plus that was kinda of a hidden suggestion to look into Linked Lists for a way to learn pointers.... don't really see how that is mind boggling unless you don't read between lines.
__________________
Only two things are infinite, the universe and human stupidity, and im not sure about the former.
drifter is offline   Reply With Quote
Old Dec 17th, 2005, 10:32 AM   #25
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Ka-ching!

Quote:
Originally Posted by para
A good practice problem is to make a function that takes a string and parses it into an array of tokens. For example:

Quote:
char** array = split("abc 123 asdf foobar");
would return

Quote:
array[0] = "abc";
array[1] = "123";
array[2] = "asdf";
array[3] = "foobar";
array[4] = NULL;
After you finished, if you used strcpy() anywhere, rewrite the function with the only allocations being for the size of the array, and the size of the initial string (hint: each index is a pointer to somewhere in a copy of the string you save).

I feel this problem is fairly good at teaching you practical uses for pointers, and a string parser is something you may see in the field.
I decided this was a nice challenge, so I made it. I wrote it in C, because in C++ there are *better* alternatives. Took me a while, because I forgot the forsaken null character :mad: hehe.
#include <stdio.h>
#include <stdlib.h>

/* returns how many words there are in the sentence */
int wordcount( char * str )
{
	int s = 0;
	char * p = str;

	while(*p != '\0')
	{
		if(*p == ' ')
			s++;
		p++;
	}
	return ++s;
}

/* returns the current word len */
int wordlen( char str[] )
{
	static int s = 0;
	int len = 0;

	if(str == NULL)
	{
		s = 0;
		return 0;
	}

	while(str[s] != '\0')
	{
		if(str[s] == ' ')
		{
			s++;
			return ++len;
		}
		s++;
		len++;
	}
	return ++len;
}

// gives location of the letters, skips whitespace
int goLetter( char str[] )
{
	static int i = 0;

	while(str[i] != '\0')
	{
		if(str[i] == ' ')
		{
			i++;
			return -1;
		}
		return(i++);
	}
	return -1;
}


/* split words and put them in an array, and return that */
char** split ( char* str )
{
	char **p;
	int i, j , x = 0;
	int len = wordcount(str);
	int curword = 0;


	/* allocate space for multi-dimensional array */
	p = malloc(len  * sizeof *p);

	/* allocation gone wrong */
	if(p == NULL)
		exit(1);

	/* allocate space for each item */
	for(i = 0; i < len; i++)
		p[i] = malloc(wordlen(str) * sizeof *p[i]);

	/* I'm a cheater :D, restart */
	wordlen(NULL);

	/* initialize */
	for (i = 0; i < len; i++)
	{
		curword = wordlen(str);
		for (j = 0; j < curword; j++)
		{
			if((x = goLetter(str)) != -1)
				*( *( p + i ) + j ) = str[x];
			else
				*( *( p + i ) + j ) = '\0';
		}
	}

	return p;
}

/* entry */
int main( void )
{
	char** array = split("abc 123 asdf foobar");
	printf("Array[0] contains %s\n", array[0]);
	printf("Array[1] contains %s\n", array[1]);
	printf("Array[2] contains %s\n", array[2]);
	printf("Array[3] contains %s\n", array[3]);
	free(array);
	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Dec 17th, 2005, 11:05 AM   #26
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Don't forget "strtok" .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 17th, 2005, 11:10 AM   #27
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
doesn't strtok only allow 1 type of delimeter? Writing you own i believe allows more freedom, no?
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Dec 17th, 2005, 11:58 AM   #28
para
Programmer
 
Join Date: Dec 2005
Posts: 65
Rep Power: 3 para is on a distinguished road
Quote:
Originally Posted by nnxion
I decided this was a nice challenge, so I made it. I wrote it in C, because in C++ there are *better* alternatives. Took me a while, because I forgot the forsaken null character :mad: hehe.
Nice job. However, it can be significantly optimized just by changing a few lines. Try modifying it so that there is no additional malloc() for each item you are creating in the array. Also your ending free() will only deallocate the array itself, not the individual items within it.

Quote:
Originally Posted by DaWei
Don't forget "strtok" .
strtok() is horrible I took a look at this function a a while ago, and it's just poor. Quoted from the man page for strtok:
BUGS
Never use these functions. If you do, note that:

    These functions modify their first argument.

    These functions cannot be used on constant strings.

    The identity of the delimiting character is lost.

    The strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you.
:eek:
para is offline   Reply With Quote
Old Dec 17th, 2005, 12:53 PM   #29
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
strtok() is horrible
Anything is horrible if you don't have the sense to study it, its use, the requirements of history, and pertinent caveats. Hot air balloons make a terrible means of transportation, but scads of people mess with them anyway, profitably and successfully. I daresay that strtok was written by people much more capable than you or I, and used my many more in the same category.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 17th, 2005, 7:07 PM   #30
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by LOI Kratong
doesn't strtok only allow 1 type of delimeter? Writing you own i believe allows more freedom, no?
No strtok allows more delimiters, see here.
I'm not sure what other 'types' of delimiters you would like.

strtok() isn't that bad..compared to gets() :p
Even DaWei agrees gets() is bad

Quote:
Originally Posted by para
Nice job. However, it can be significantly optimized just by changing a few lines. Try modifying it so that there is no additional malloc() for each item you are creating in the array.
I'm not sure how I'd get the subscript right, without additional allocation.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:37 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC