Jump to content
  • Advertisement
MarcusAseth

to_lower function entering an infinite loop

This topic is 421 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am super confused by this...

I was in the middle of an experiment, maybe is just that I am tired and I'm missing the obvious here (so facepalm is ready), but things are not working as expected, Image below (I'll also paste the code in case you want to try it out yourself)

#include <iostream>
#include <ostream>
using namespace std;

void to_lower(char* s)
{
	char* text = s;
	while (s != '\0')
	{
		char letter = *s;
		if (letter > 'A' && letter < 'Z')
		{
			*s = 'a' + (letter - 'A');
		}
		s++;
	}
	cout << text << endl;
}

int main()
{
	char* text = new char[12] {"Hello World"};
	to_lower(text);
	delete[] text;
	return 0;
}

As you see in the debug image, the content of s[0] is 'l', but "char letter = *s;" is yelding a 'e'...what is this madness?! O_O

 

Untitled-1.png

 

EDIT: forget what I asked above, I just understood that the yellow arrow means that the line isn't executed yet, I just had to step forward once :|

So, since that question turned useless I renamed this topic and I have a new question: my function enters an infinite loop.

Apparently the test "while (s != '\0')" is not good because when s points to '\0', the content of s is "" according to the debugger... so how do I test for '\0'? :P

 

Edited by MarcusAseth

Share this post


Link to post
Share on other sites
Advertisement

You're comparing a pointer, i.e. a memory address, against what you're expecting to see when dereferencing the pointer, i.e. the number 0 or '\0'. You need to dereference the pointer.

Also, your if statement is wrong.

And, you're wasting your time duplicating a standard language function.

Share this post


Link to post
Share on other sites
10 minutes ago, Kylotan said:

You're comparing a pointer, i.e. a memory address, against what you're expecting to see when dereferencing the pointer, i.e. the number 0 or '\0'. You need to dereference the pointer.

Also, your if statement is wrong.

And, you're wasting your time duplicating a standard language function.

ops, missed that dereference in the while, I knew I was tired x_x

Fixed my >= and <= , thanks! :D

I disagree on the last point though, I don't think this time is wasted, we are probably assigning value to different things :P

Edited by MarcusAseth

Share this post


Link to post
Share on other sites
9 minutes ago, Alpha_ProgDes said:

Though you wouldn't do this in production code, I agree you should know/understand how to handle pointers.

Exactly my goal here :P

What I'm currently writing is probably ugly and a bit buggy, but I'm getting useful practice, today first time I use const_cast and write my template, so...IMPROVEMENT (for better or worse...):D

char* findx(const char* s, const char* x)
{
	if(s == nullptr || x == nullptr)
	{ return nullptr; }

	char* Tmp = const_cast<char*>(x);
	int FindSize = 0;
	while (*Tmp++ != '\0') { FindSize++; };

	int ID = 0;
	int firstOccurrence = 0;
	bool isMatch = false;
	
	while (s[ID] != '\0')
	{
		if (s[ID] == x[0])
		{
			firstOccurrence = ID;
			for (size_t i = 0; i < FindSize; i++)
			{
				if (s[ID + i] != x[i]) { break; }
				if (i == FindSize - 1) { isMatch = true; }
			}
			if (isMatch) { break; }
		}
		ID++;
	}
	return (isMatch ? const_cast<char*>(s) + firstOccurrence : nullptr);
}

 

Edited by MarcusAseth

Share this post


Link to post
Share on other sites

Using const cast looks horribly wrong, "find" shouldn't change the string!

You have "char *tmp" and return a "char *",  change that to "const char *" too.

 

At a higher level, you are accepting const char * values as parameters, users don't expect that you break your promise, and sneakily return a modifiable point in their const string as return value.

Edited by Alberth

Share this post


Link to post
Share on other sites

The Exercise in the book I'm following gave as only instructions:

Quote

Write a function, char* findx(const char* s, const char* x), that finds the first occurrence of the C-style string x in s.

I don't know how to return a char* out of a const char*, so I tried removing the const, just to fullfill the assignment :P

Edited by MarcusAseth

Share this post


Link to post
Share on other sites

Here you go friend

//take two char* (strings) and return char* to first occurence of second string
char* findx(char* str, char* strx ) {
	
	if (!str || !strx) return nullptr;

	char* found = nullptr;

	for (int s = 0, x = 0; str[s] != '\0' && strx[x] != '\0'; s++) {

		if (str[s] == strx[x]) {
			if( !found ) found = &str[s];
			x++;
		} else {
			found = nullptr;
			x = 0;
		}
	}

	return found;
}

 

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!