Problem with Pointer to Char Array

I cannot get my whole char array to print out by calling the cout function on the pointer to the array. The pointer prints the whole array when called before the return call in the function, and afterwards the *pointer call does in fact print the first char in the array, but just calling the pointer gives garbage. Anyone know where I am going wrong? Please see code and output below.

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
78
79
80
81
82
83
84
85
86
87
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <string.h>
using namespace std;

class card{
	int suit; //Hearts=1,Clubs=2,Spades=3.Diamonds=4
	int cardnum; //A=1,2-10=2-10,J=11,Q=12,K=13
public:
	card(int s,int cn);
	int getSuit();
	int getCardNum();
	char* toString();
};

card::card(int s,int cn){
	suit = s;
	cardnum = cn;
}

int card::getSuit(){
	return suit;
}

int card::getCardNum(){
	return cardnum;
}

char* card::toString(){
	char str[20] = "";
	char numstr[6];
	char conststr[5] = " of ";
	char suitstr[9];

	switch(cardnum){
	case 1:
		strcpy_s(numstr,"Ace");
		break;
	case 11:
		strcpy_s(numstr,"Jack");
		break;
	case 12:
		strcpy_s(numstr,"Queen");
		break;
	case 13:
		strcpy_s(numstr,"King");
		break;
	default:
		_itoa(cardnum,numstr,10);
		break;
	}

	switch(suit){
	case 1:
		strcpy_s(suitstr, "Hearts");
		break;
	case 2:
		strcpy_s(suitstr, "Clubs");
		break;
	case 3:
		strcpy_s(suitstr, "Spades");
		break;
	case 4:
		strcpy_s(suitstr, "Diamonds");
		break;
	}

	strcat_s(str,numstr);
	strcat_s(str,conststr);
	strcat_s(str,suitstr);

	char *s;
	s = str;
	cout << s << '\n';
	return s;
}


int _tmain(int argc, _TCHAR* argv[])
{
	card c(1,1);
	char *s = c.toString();
	cout << *s << '\n';
	cout << s << '\n';
	return 0;
}



Ace of Hearts
A
╠╠╠╠


I do not understand the last line of output. Why is it printing that?

Thanks,
Kevin U.
I'm pretty sure it's because str is a local variable to the function, pointing at an array of characters that is destroyed when the function closes. So when you return s, you are basically returning a pointer to memory that has been freed.
what Zhuge said is correct. char *s before you send it is pointing to the data in str, so when you leave the function, it's gone... you have to create new space and copy the data over...

something like (I chose 20 because that's the size of str)...

 
char *s = new char[20];


and then copy str into it, but NOT s = str; which doesn't do that.


as a side note, the char *s that receives the new data should be deleted in practice even though it probably doesn't need it since it's right before you leave... either way,

1
2
delete [] s;
s = 0;



that set's 's' to null also so that you don't have double deletes as a precaution
Thanks for your help. I decided to use the string class.
Topic archived. No new replies allowed.