Comp Sci C++ compare strings program using pointers

AI Thread Summary
The discussion focuses on creating a C++ function, isEqual(), to compare two strings using pointers. The initial code contains errors, particularly in the comparison logic, where it mistakenly checks if the pointers themselves are equal instead of the values they point to. Corrections include changing the comparison to dereference the pointers and simplifying the logic for setting the comparison result. Additionally, there is clarification on how function pointers work in C++, emphasizing that using the function name alone refers to its address rather than its return value. The conversation highlights common pitfalls in pointer usage and function overloading in C++.
testme
Messages
68
Reaction score
0

Homework Statement


Create a function called isEqual(). isEqual() takes two strings and determines if the strings are identical. isEqual (s1, s2) should be the same as s1 = = s2

Code:
#include "stdafx.h"
#include <iostream>
using namespace std;

bool isEqual(int *s, int *st, int x);
int _tmain(int argc, _TCHAR* argv[])
{
	int string1[5] = {1, 2, 3, 4, 5};
	int string2[5] = {1, 2, 3, 4, 5};
	for(int i = 0; i < 5; i++)
	{
		cout << string1[i];
	}
	cout << endl;
	for(int h = 0; h < 5; h++)
	{
	 cout << string2[h];
	}
	cout << endl;
	isEqual(string1, string2, 5);
	if(isEqual)
	{
	cout << "The two strings are not the same." << endl;
	}
	else
	{
	cout << "The two strings are the same." << endl;
	}
	system("pause");
	return 0;
}
bool isEqual(int *s, int *st, int x)
{
	bool same = true;
	for(int i = 0; i < x; i++)
	{
		same = true;
		if(s == st)
		{
		same = true;
		}
		else
		{
		same = false;
		break;
		}
	s++;
	st++;
	}
	if(same == true)
	{
	return true;
	}
	else
	{
	return false;
	}
}
Sorry if my code is overly confusing for what it's supposed to do.

What I want it to do is paste the two strings then it goes to the function compares then, returns true or false and pastes whether they are the same or not. Howver it's not reading any of them the same. Anyone know where I'm going wrong?
 
Physics news on Phys.org
Code:
    if(s == st)
That should be:
Code:
    if(*s == *st)
Also, there is no need to keep setting same=true, you only need to do that one time, only changing it to false if there is a mis-compare.
 
I fixed that but it still doesn't work right, it still says the strings are not the same
 
update - I updated this post.

Code:
isEqual(string1, string2, 5);
	if(isEqual)
That should be:
Code:
    if(isEqual(string1, string2, 5) != true)
 
Last edited:
I get what you did, though my teacher said something about you can shorten it to just isEqual under a certain circumstance, am I misusing that and do you know when it can be done?
 
testme said:
I get what you did, though my teacher said something about you can shorten it to just isEqual under a certain circumstance, am I misusing that and do you know when it can be done?
The first issue is if(...) is the same as if(0 != ...), so you had the sense of the if reversed in your original code. I'm not sure when you can just use isEqual. One issue with C++ is that you can have multiple functions with the same name isEqual, where the only diference is the parameters, for example:

bool isEqual(char *, char *, int); // compare two arrays of characters
bool isEqual(int *, int *, int); // compare two arrays of integers
bool isEqual(double *, double *, int); // compare two arrays of doubles
bool isEqual(char *, char*); // compare two strings terminated by zero
bool isEqual() // always return true if no parameters

"isEqual" by itself means the address of a function. Normally only used when assigning a pointer to function. Although you can overload functions, I don't think you can overload pointer to functions, so the pointer type determines which function to point to:

bool ((*pointertofunctionarrayofchar)(char *, char *, int) = isEqual;
bool ((*pointertofunctionarrayofdouble)(double *, double *, int) = isEqual;
 
Last edited:
Okay, and if I'm understanding you right so then it would be if(isequal) would in a sense mean if(isequal = true) and then I'd have they are the same else they are not the same?
 
testme said:
if(isEqual == true)
isEqual is the address of the function, not it's previous return value. If the function isEqual() is stored in memory starting at 0x411400, then your if statement is the same as if(0x411400 == true).
 

Similar threads

Replies
8
Views
1K
Replies
7
Views
2K
Replies
3
Views
2K
Replies
2
Views
2K
Replies
14
Views
5K
Replies
1
Views
1K
Back
Top