Click here to Skip to main content
15,891,828 members

Response to: Help Pointers and chars in C++;

Revision 1
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 )
   {
      strncpy(szName, x, BUFF_SIZE);
   }

}


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 dearch on CodeProject will help you..
Posted 13-Nov-12 1:31am by Andy411.
Tags: