Problem with a pointer

Hello, I'm making a little program that checks for palindromes. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <cstring>
#include <iostream>

#define FALSE    0
#define TRUE     1

#define PALIN    1
#define DPALIN   2
#define IPALIN   3
#define NONE     0

using namespace std;

void Turn_Round(char *Word, char *(&Return))
{
	int Size_W = strlen(Word);
	int Size_R = strlen(Return);
	int count = 0;
	
	for(int i = Size_W - 1; i > -1; i--)
	{
		Return[count] = Word[i];
		count++;
	}
}

int is_equal(char *W1, char *W2)
{
	int S1 = strlen(W1);
	int S2 = strlen(W2);
	if(S1 != S2) return FALSE;
	
	int count = 0;
	for(int i = 0; i < S1; i++)
	{
		if(W1[i] != W2[i])
		{
			count++;
			break;
		} 
	}
	if(count > 0) return FALSE;
	return TRUE;
}

int Palin(char *Word)
{
	cout << " > The word is : " << Word << endl;
		
	char *Use;

	Turn_Round(Word, Use);
	cout << "   > The word turned around is : " << Use << endl;
	if(is_equal(Word, Use))
	{
		cout << "   > The word is a palindrome...\n";
		return PALIN;
	}
	else
	{
		cout << "   > The word is not a palindrome...\n";
	}
	
	//char *Temp;
	return NONE;
}

int main(int argc, char *argv[])
{
	char Word[20];
	cout << " > Insert a word : ";
	cin >> Word;
	
	int i = Palin(Word);
	
	return 0;
}


The thing is that the line commented out in the function Palin, the "char *Temp;" causes a segmentation fault. I need to create a new char* to keep checking for other stuffs in the string, I thought the segmentation fault was because I was initializing a pointer and then I didn't use it, but even if I initialize the pointer, or use it, it keeps giving the segmentation fault!

It is surely a really simple error, but I'm not a pretty much experienced programmer, so I can't find out what it is. Thanks! And sorry for my bad english haha.
This is causing the segfault
1
2
3
4
5
6
7
char *Use; //you aren't reserving memory
Turn_Round(Word, Use);
void Turn_Round(char *Word, char *(&Return))
{
	//...
	Return[count] = Word[i]; //trying to access non reserved memory
}

1
2
3
4
//to reserve memory
char *Use = new char[the_space_you_want];
//and remember to delete it
delete [] Use;

But It would be a lot better if you just use strings.
Okey. Thank you very much. I can't believe it was that hahaha, I guess I'll have to check my code a little bit more before posting on the forum :P
You could avoid the C style code and make things a lot simpler:

1
2
3
4
5
6
7
8
9
10
bool isPalindrome(const std::string& s) /* notice that C++ has a built-in type bool,
                                           so there's no need for macro hacks */
{
    std::string temp;
    for(unsigned int i = 0; i < s.size(); ++i)
    {
        temp += s[s.size() - 1 - i];
    }
    return s == temp;
}

You could simplify that even more with a standard library function, but I guess that would defeat the purpose of the exercise.
Topic archived. No new replies allowed.