Archived

This topic is now archived and is closed to further replies.

bony

need help (pointer - related)

Recommended Posts

hi everybody! i've written a small function, that kind of formats strings in the console, so no words are broken up, when the line ends. here it is:
void formattext(char *text, int columns)
{
	while(1)
	{
		for(int i=1; i<columns && *text; i++)
			text++;
		if(!*text)
			break;
		while(*text != ' ')
			text--;

		*text = '\n';        // <- PROBLEM

	}
}
in runtime, i get a memory error there.... can't i change the value at the adress of the pointer?? and if not, why, and how can i? thank you! bony [edited by - bony on October 7, 2003 3:08:18 PM]

Share this post


Link to post
Share on other sites
Are you sure you are not bumping the value of 'text' beyond the valid range of the string? It looks to me that if your string has no whitespace at all, the while(*text != ' ') text-- could conceivably decrement the text ptr all the way back to the start of the string and beyond, until it hits the first random byte equal to SPACE in memory preceding the string. You should put a check to make sure 'text' doesn't back too far.

As long as your pointer doesn't stray out of bounds, *text = '\n' should work just fine.

Josh
vertexnormal AT linuxmail DOT org


Check out Golem: Lands of Shadow, an isometrically rendered hack-and-slash inspired equally by Nethack and Diablo.

[edited by - VertexNormal on October 7, 2003 3:38:35 PM]

Share this post


Link to post
Share on other sites

void formattext(char *text, int columns)
{
int counter = 0;
char* lastspace = NULL;
while( *text )
{
if( *text == ' ' ) lastspace = text;

if( counter >= columns && lastspace )
{
text = lastspace + 1;
*lastspace = '\n';
lastspace = NULL;
counter = 0;
}
else
{
++counter;
++text;
}
}
}


Note that if a word is longer than columns , it will be on its own but will still spill over several lines.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on October 7, 2003 8:08:00 PM]

Share this post


Link to post
Share on other sites
thanks for the better version of the function, fruny,

but it still crashes on the line

*lastspace = ''\n'';


it says "access violation" at some memory address...

i also thought that was a correct line, but it always crashes, i tried it on two PCs...

Share this post


Link to post
Share on other sites
Are you trying to pass it a pointer to string literal ( boom ! you cannot *modify* those ), or an array containing a string ( as you should) ?

Edit : also note that I replaced 'counter == columns' with 'counter >= columns'


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on October 7, 2003 8:07:49 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by bony

void formattext(char *text, int columns)
{
while(1)
{
for(int i=1; i<columns && *text; i++)
text++;
if(!*text)
break;
while(*text != ' ')
text--;

*text = '\n'; // <- PROBLEM

}
}



That is the most evil abuse of loops I have ever seen.

[edited by - SoulSkorpion on October 7, 2003 12:44:07 AM]

Share this post


Link to post
Share on other sites
ok, i got the mustake now - was a stupid mistake by myself..

the string i passed to the function was a pointer instead of an array.
well, i''m just a newbie after all

thanks allot for your help, fruny!!

"That is the most evil abuse of loops I have ever seen."

-> why is that?

thanks, bony

Share this post


Link to post
Share on other sites