to_lower function entering an infinite loop

Started by
27 comments, last by MarcusAseth 6 years, 7 months ago

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

 

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.

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

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

Beginner in Game Development?  Read here. And read here.

 

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);
}

 

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.

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

Book is just wrong :)

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;
}

 

@h8CplusplusGuru much more clean/compact than mine, regardless if I had done it without the const char required by the exercise, thanks for showing me :)

 

This topic is closed to new replies.

Advertisement