BleepingComputer.com: Pointer Arithmetic

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.

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Pointer Arithmetic

#1 User is offline   danbrownlow 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 99
  • Joined: 05-March 07
  • Gender:Male

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.

#2 User is offline   Billy O'Neal 

  • Bleepin Engineer
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Instructor
  • Posts: 10,086
  • Joined: 17-January 08
  • Gender:Male
  • Location:Cleveland, Ohio

Posted 25 April 2009 - 08:47 PM

Which loop is it getting stuck in?

Billy3

#3 User is offline   Romeo29 

  • Learning To Bleep
  • PipPipPipPipPipPip
  • Find Topics
  • Group: BC Advisor
  • Posts: 2,814
  • Joined: 06-July 08
  • Gender:Not Telling
  • Location:127.0.0.1

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.

This post has been edited by Romeo29: 25 April 2009 - 11:28 PM

[url="http://www.avast.com/"]avast! free antivirus[/url]

#4 User is offline   danbrownlow 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 99
  • Joined: 05-March 07
  • Gender:Male

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 User is offline   Billy O'Neal 

  • Bleepin Engineer
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Instructor
  • Posts: 10,086
  • Joined: 17-January 08
  • Gender:Male
  • Location:Cleveland, Ohio

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users