Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / C++

Help Pointers and chars in C++;

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
13 Nov 2012CPOL 4.9K   1  
Hey.When I do this:char *suit = "Hearts"; coutusing namespace std;class yup{public: yup(char *); void print();private: char *name;};yup::yup(char...

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
13 Nov 2012Andy411
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;...
Please Sign up or sign in to vote.
13 Nov 2012CPallini
Quote:cout<<*name;You should write insteadcout << 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...
Please Sign up or sign in to vote.
13 Nov 2012Orjan Westin
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...

License

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


Written By
Student
Lebanon Lebanon
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions