C++ compare strings program using pointers

Click For Summary

Discussion Overview

The discussion revolves around a C++ programming task involving the creation of a function called isEqual() that compares two strings (or arrays) to determine if they are identical. The focus is on debugging the implementation and understanding function pointers and comparisons in C++.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant presents a code snippet for the isEqual() function and describes its intended functionality, noting confusion about its current behavior.
  • Another participant suggests changing the comparison from if(s == st) to if(*s == *st) to correctly compare the values pointed to by the pointers.
  • A participant mentions that the variable same does not need to be reset to true in each iteration of the loop, as it should only change to false upon a mismatch.
  • Further clarification is provided on how to correctly check the return value of isEqual() in the if statement.
  • One participant asks about the conditions under which the function name can be used without parentheses, indicating a misunderstanding of function pointers.
  • Another participant explains the concept of function overloading in C++, noting that multiple versions of isEqual() can exist with different parameter types.
  • A later reply clarifies that using if(isEqual) refers to the address of the function rather than its return value, which contributes to the confusion.

Areas of Agreement / Disagreement

Participants express differing views on the correct implementation of the isEqual() function and its usage. There is no consensus on the final solution, as multiple issues and misunderstandings are raised throughout the discussion.

Contextual Notes

Participants highlight limitations in understanding function pointers and the nuances of C++ syntax, particularly in relation to function overloading and comparisons. The discussion does not resolve these misunderstandings.

Who May Find This Useful

Readers interested in C++ programming, particularly those learning about function implementation, pointers, and debugging techniques, may find this discussion relevant.

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 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K