Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey.When I do this:
char *suit = "Hearts"; cout<<*suit; it prints Hearts;

But in this case: It is only printing "m". WHY?
C++
#include "stdafx.h"
#include <iostream>
using namespace std;

class yup{
public:
	yup(char *);
	void print();
private:
	char *name;
};
yup::yup(char *x)
{
	name = x;
}
void yup::print()
{
	cout<<*name;
	cout<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
	yup t("missak");
	t.print();
	return 0;
}
Posted
Updated 13-Nov-12 1:20am
v2

Here you are only assigning the adress of x into name. And at the adress of x is the 'm' of "missak".

C++
yup::yup(char *x)
{
	name = x;
}


Here the content of the adress "name" is printed. And the content is a single char. It's the letter 'm'.
C++
void yup::print()
{
    cout<<*name;
    cout<<endl;
}



You have to copy the whole string into your private member.
In C you could do this like this e.g.
C++
char szName[BUFF_SIZE];

void yup(const char *x)
{
   if( strlen(x) < BUFF_SIZE )
   {
      strcpy(szName, x);
   }

}


In C++ there is also a class for strings. I don't have a good reference right here at the moment. But a little google or search on CodeProject will help you..
 
Share this answer
 
v2
Comments
Richard MacCutchan 14-Nov-12 11:27am    
Look again at the declaration of name.
Andy411 19-Nov-12 2:50am    
Sorry, I do not understand your hint???
I know, that my solution is not perfect, because I did not use the string class, but I hoped it explains the effect the questioner noticed.
Richard MacCutchan 19-Nov-12 3:35am    
Well I can't remember exactly what was in my mind at the time but your first explanation states "Here you are only assigning the adress of x into name.". I would say that is a bit confusing as what the code is doing is setting the name variable to point to the same object or memory address as is in variable x. Your second comment does not explain dereferencing of pointers clearly. And your final comment is not necessarily the right thing; it may well be that saving the pointer is sufficient, it all depends on the context.
Andy411 19-Nov-12 3:55am    
Thx for the reply.
Well, for me and my bad English it is sometimes difficult to explain things correct. I just tried to clarify the effect and show some possible solution. An additional challange for me is the fact, that C++ is a bit out of focus for me. The C and C++-days are 3 years ago. Since then I programme most of the time in C#.
CPallini 19-Nov-12 4:02am    
My 5. Like you I have no idea why someone voted your answer 1.
Quote:
cout<<*name;

You should write instead
C++
cout << name;


By the way you shouldn't store a pointer to the original character array (it could be a temporary). You should instead copy the array content. Even better you could use a std::string:
C++
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class yup{
public:
	yup(char *x ):name(x){};
	void print();
private:
	string name;
};
void yup::print()
{
	cout << name;
	cout << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
	yup t("missak");
	t.print();
	return 0;
}
 
Share this answer
 
v3
Comments
Andy411 19-Nov-12 2:51am    
My 5! That's it with using the string class. The little but not less important thing I left out in my solution.
BTW: I don't know, why some idiot voted 1 for your solution.
CPallini 19-Nov-12 4:01am    
Thank you.
When I do this:
C++
char *suit = "Hearts"; cout<<*suit;

it prints Hearts;

It shouldn't, it should print the character 'H' only. What you do is, you dereference the pointer-to-char, so it is only a char. That's why it's only the char that is printed from your class.

If you wish the class to print the whole string, don't dereference the char pointer.
 
Share this answer
 
v2
Comments
missak boyajian 13-Nov-12 7:27am    
what do you mean by not dereferencing?
Orjan Westin 13-Nov-12 8:19am    
In your print function, you dereference the char pointer "name", by using the dereferencing operator *. If you want to print the string the char pointer is pointing to, you should use it without modification:

void yup::print()
{
cout << name;
cout << endl;
}
missak boyajian 13-Nov-12 14:05pm    
Thank you.It worked. But I thought that when I print only name, it will give me the address number. In this case it gave the string why?
Orjan Westin 13-Nov-12 14:51pm    
If you have a pointer to something else, say int* or something, it will print the address. But there's a special handling of char* to print all the characters from that address and forward until a byte containing zero (0) is encountered. This is to maintain how strings are handled in C.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900