Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hey.When I do this:
char *suit = "Hearts"; cout<<*suit; it prints Hearts;
 
But in this case: It is only printing "m". WHY?
#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 13-Nov-12 1:16am
Edited 13-Nov-12 1:20am
Andy4112.6K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Here you are only assigning the adress of x into name. And at the adress of x is the 'm' of "missak".
 
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'.
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.
 
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..
  Permalink  
v2
Comments
Richard MacCutchan at 14-Nov-12 11:27am
   
Look again at the declaration of name.
Andy411 at 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 at 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 at 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 at 19-Nov-12 4:02am
   
My 5. Like you I have no idea why someone voted your answer 1.
Andy411 at 19-Nov-12 5:09am
   
Thx, too.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Quote:
cout<<*name;

You should write instead
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:
#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;
}
  Permalink  
v3
Comments
Andy411 at 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 at 19-Nov-12 4:01am
   
Thank you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

When I do this:
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.
  Permalink  
v2
Comments
missak boyajian at 13-Nov-12 7:27am
   
what do you mean by not dereferencing?
Orjan Westin at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 6,696
1 Sergey Alexandrovich Kryukov 6,675
2 CPallini 5,315
3 George Jonsson 3,584
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100