By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

why doesnt this work?

P: n/a
#ifndef PERSON_H
#define PERSON_H

#include<iostream>
#include<iomanip>
#include<string>

class Person {
protected:
char name[12];
name = jamesbond;

public:
Person();
void Print();
};//close Person
#endif

or this:

#include "person.h"

Person::Person()
{
name = "jamesbond";
cout<<"The student's name is: "<<name;
}

--------------------------------------------------
*** E-mail is shut off ***
--------------------------------------------------
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,

Either use string (preffered way):

string name( "jamesbond" );

Or

strncpy( name, "jamesbond", sizeof name );
or
char *name="jamesbond";

Regards, Ron AF Greve
]
"Developwebsites" <de*************@aol.com> wrote in message
news:20***************************@mb-m27.aol.com...
#ifndef PERSON_H
#define PERSON_H

#include<iostream>
#include<iomanip>
#include<string>

class Person {
protected:
char name[12];
name = jamesbond;

public:
Person();
void Print();
};//close Person
#endif

or this:

#include "person.h"

Person::Person()
{
name = "jamesbond";
cout<<"The student's name is: "<<name;
}

--------------------------------------------------
*** E-mail is shut off ***
--------------------------------------------------

Jul 22 '05 #2

P: n/a
"Moonlit" <al******@jupiter.universe> wrote...
Either use string (preffered way):

string name( "jamesbond" );
Since 'name' is a member, you cannot initialise it that way.
It has to be done in the constructor member initialisation
list. Nonetheless, using string is A VERY GOOD IDEA(tm).

Or

strncpy( name, "jamesbond", sizeof name );
Why are you copying 'sizeof name' bytes? What if there are
more elements in 'name' than 10? You will try accessing memory
beyond the end of the string literal, which causes undefined
behaviour. If you need to copy, (a) make sure you have enough
elements in 'name' and (b) use 'strcpy', which only copies as
many chars as there are in the second argument, plus the
terminating null character.

strcpy(name, "jamesbond");

Of course, if there is not enough elements in 'name', you end
up overrunning memory, which is also undefined behaviour. So,
all things considered, strings are much better alternative,
aren't they?
or
char *name="jamesbond";
It's much better to NEVER declare a pointer to _non-const_ char
and initialise it with a literal. This nonsense is dragging
into C++ from the old C days, and should be avoided. Use

const char * name = "jamesbond";

And, again, if it's a member, it cannot be initialised that way,
blah blah blah

Just my $0.04...

Regards, Ron AF Greve
]
"Developwebsites" <de*************@aol.com> wrote in message
news:20***************************@mb-m27.aol.com...
#ifndef PERSON_H
#define PERSON_H

#include<iostream>
#include<iomanip>
#include<string>

class Person {
protected:
char name[12];
name = jamesbond;

public:
Person();
void Print();
};//close Person
#endif

or this:

#include "person.h"

Person::Person()
{
name = "jamesbond";
cout<<"The student's name is: "<<name;
}

--------------------------------------------------
*** E-mail is shut off ***
--------------------------------------------------


Jul 22 '05 #3

P: n/a
On Sun, 14 Dec 2003 19:57:52 +0000, Victor Bazarov wrote:

strncpy( name, "jamesbond", sizeof name );


Why are you copying 'sizeof name' bytes? What if there are more
elements in 'name' than 10? You will try accessing memory beyond the
end of the string literal, which causes undefined behaviour. If you
need to copy, (a) make sure you have enough elements in 'name' and (b)
use 'strcpy', which only copies as many chars as there are in the second
argument, plus the terminating null character.


This is wrong. strncpy(dst,src,n) will stop when either a null character
is found in src or when n bytes have been copied, hence there is no
undefined behaviour here (but note that strncpy doesn't add a terminating
null to dst if strlen(src) >= n).
Jul 22 '05 #4

P: n/a
The reason this doesnt work is because you declared name[12] as a char
array, which doesnt have a = operator. as mentioned before, you have 2
options, 1. declare name as a string. 2. use strcpy/strncpy instead of the =
operatior
"Developwebsites" <de*************@aol.com> wrote in message
news:20***************************@mb-m27.aol.com...
#ifndef PERSON_H
#define PERSON_H

#include<iostream>
#include<iomanip>
#include<string>

class Person {
protected:
char name[12];
name = jamesbond;

public:
Person();
void Print();
};//close Person
#endif

or this:

#include "person.h"

Person::Person()
{
name = "jamesbond";
cout<<"The student's name is: "<<name;
}

--------------------------------------------------
*** E-mail is shut off ***
--------------------------------------------------

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.