Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Pointer Arithmetic


  • Please log in to reply
4 replies to this topic

#1 Guest_danbrownlow_*

Guest_danbrownlow_*

  • Guests
  • OFFLINE
  •  

Posted 25 April 2009 - 03:32 PM

Hey, I've decided to go back and learn C++ so I dug out my old book and found the following listing, but it gets stuck in the loop. I've checked and checked and can't see anything wrong :'(

#include <iostream>
#include <ctype.h>
#include <string.h>

using namespace std;

bool GetWord(char* theString, char * word, int& wordOffset);

int main() {
	const int bufferSize = 255;
	char buffer[bufferSize+1]; //hold the entire string
	char word[bufferSize+1];   //hold the word
	int wordOffset = 0;		//start at the beginning

	cout << "Enter a string: ";
	cin.getline(buffer,bufferSize);

	while (GetWord(buffer, word, wordOffset))
	{
		cout << "Got this word: " << word << endl;
	}
	return 0;
}

//function to parse words from a string
bool GetWord(char* theString, char* word, int& wordOffset)
{
	if (theString[wordOffset] == 0) //end of string
		return false;

	char *p1, *p2;
	p1 = p2 = theString + wordOffset; //Point to the next word

	//eat leading space
	for (int i = 0; i < (int)strlen(p1) && !isalnum(p1[0]); i++)
		p1++;

	//see if you have a word
	if(!isalnum(p1[0]))
		return false;

	//p1 now points to start of next word
	//p2 needs to point there to
	p2 = p1;

	//march p2 to end of the word
	while(!isalnum(p2[0]))
		p2++;

	//p1 at beginning
	//p2 at the end
	//length of word is the difference
	int len = int (p2 - p1);

	//copy word into the buffer
	strncpy (word,p1,len);

	//null terminate at
	word[len]='\0';

	//now find the beginning of the next word
	for (int j = int(p2-theString); j<(int)strlen(theString)
		&& !isalnum(p2[0]); j++)
	{
		p2++;
	}

	wordOffset = int(p2-theString);

	return true;
}

I'm sorry if it's something obvious, I just got back from Vietnam yesterday so feeling a little jet-lagged hehe.

BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington

Posted 25 April 2009 - 08:47 PM

Which loop is it getting stuck in?

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:08:00 PM

Posted 25 April 2009 - 11:12 PM

in C++ you dont use ctype.h but isalnum() is declared in <locale>

http://www.cplusplus.com/reference/clibrary/cctype/isalnum/

You use it like this:
#include <iostream>
#include <locale>
int main(){
locale loc("German_Germany");
cout << isalnum('_',loc);
return 0;
}

I found an error on this
while(!isalnum(p2[0]))	 p2++;

This code increments p2 pointer if charcater is not alphnumeric, which means nothing happens if a word is found. as a result p1 = p2 and len=0.

Edited by Romeo29, 25 April 2009 - 11:28 PM.


#4 Guest_danbrownlow_*

Guest_danbrownlow_*

  • Guests
  • OFFLINE
  •  

Posted 26 April 2009 - 12:41 PM

Hey, thanks for that!

The error you pointed out was the one that was making the whole program go crazy (^_^)Y

In regards to ctype.h and locale.. Is it best to use locale?

Thanks, Dan.

#5 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:10:00 PM

Posted 26 April 2009 - 12:54 PM

It shouldn't matter so long as you don't mix and match C++ locale dependant stuff with C stuff.

isalnum shouldn't be affected by locale anyway... a space is a space is a space. However, with other functions, such as tolower(), the difference can be substantial.

C++ locales are explained here:
http://www.cantrip.org/locale.html

Hope that helps,

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users