Click here to Skip to main content
Sign Up to vote bad
good
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:16
Edited 13 Nov '12 - 1:20
Andy4112.5K


3 solutions

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  
Comments
Andy411 - 19 Nov '12 - 2:51
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:01
Thank you.
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  
Comments
Richard MacCutchan - 14 Nov '12 - 11:27
Look again at the declaration of name.
Andy411 - 19 Nov '12 - 2:50
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:35
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:55
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:02
My 5. Like you I have no idea why someone voted your answer 1.
Andy411 - 19 Nov '12 - 5:09
Thx, too.
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  
Comments
missak boyajian - 13 Nov '12 - 7:27
what do you mean by not dereferencing?
Orjan Westin - 13 Nov '12 - 8:19
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:05
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:51
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 294
1 OriginalGriff 195
2 CPallini 163
3 Mahesh Bailwal 159
4 Tadit Dash 148
0 Sergey Alexandrovich Kryukov 10,169
1 OriginalGriff 7,749
2 CPallini 4,181
3 Rohan Leuva 3,482
4 Maciej Los 3,089


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