need help (pointer - related)

Started by
5 comments, last by bony 20 years, 6 months ago
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]
:) if you found any spelling errors, you may keep them. :)
Advertisement
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]
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]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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...

:) if you found any spelling errors, you may keep them. :)
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]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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]
-------------"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."- Charles Babbage (1791-1871)
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
:) if you found any spelling errors, you may keep them. :)

This topic is closed to new replies.

Advertisement