In this class,
line 3 "char * pName" and last line "char * name"
pName and name are pointers or strings?
I think they are pointers but if they are pointers how can we do
strlen(pName) and strcpy(name,pName) in the code?
Thanks

class student
{
public:
student(char * pName="no name",int ssId=0)
{
id=ssId;
name=new char[strlen(pName)+1];
strcpy(name,pName);
cout < <"construct new student " < <pName < <endl;
}

void copy(student & s) 
{
    if(this==&s)
{
    cout < <"wrong" < <endl;
    return;
}
    else
{
    name=new char[strlen(s.name)+1];       
        strcpy(name,s.name);
        id=s.id;
    cout < <"successful" < <endl;
}

}
void disp()
{
cout < <"Name:" < <name < <"  Id:" < <id < <endl;
}
~student()
{
cout < <"Destruct " < <name < <endl;
delete name;
}
private:
int id;
char * name;
};

char * is a C style string (versus a std::string in C++), also a pointer to char (related to them being considered arrays of char). C strings must be null terminated (that is end in the character '\0') and they are immutable (you can't change a character while leaving the string intact).

The header file (#include <cstring>) has the functions strcpy and strcmp among others, almost all of which take char * as the types of their parameters. See this for a reference.

Also, please use the code tags:

[code]

//code goes here

[/code]

Member Avatar for r.stiltskin

In the last line of your class definition, char *name is just a pointer. char * is the data type, and name is the name of this particular pointer.

In the other examples you cited it's a little more complicated. Those are parameters (or "arguments") that are being passed to functions. A C-string is a special form of character array: an array of chars in which the last element is the null character '\0' which is used as a flag to indicate the end of the string. In C and C++, when we pass an array (a C-string or any other kind of array) to a function we don't actually pass an entire copy of the contents of the array. Instead we just pass a pointer -- the address of the array.

So, yes, in your other examples (the student class constructor and the C string functions strlen and strcpy), the parameter char *pName is a pointer, but the functions have been written so as to treat the pointer as the memory address of the beginning of a char array (i.e. a string). If you write a program that passes to one of these functions a char * that is NOT a pointer to an actual string, it will compile without reporting an error, but you will have an error when you run the program because the function will not find a null-terminated string at that address.

Two follow up questions
(1) shouldn't we use "delete [] name" instead of "delete name"
(2) why "name=new char[strlen(pName)+1];" is a must? If I comment out this line, there shows errors when i run the code

thanks

Two follow up questions
(1) shouldn't we use "delete [] name" instead of "delete name"
(2) why "name=new char[strlen(pName)+1];" is a must? If I comment out this line, there shows errors when i run the code

thanks

(1) You are correct on that AFAIK.
(2) For the reason that r.stiltskin referred to above. I may have been misleading when I said the char * is the string. Technically it truly is a pointer. When you have "char * str" all you have is a pointer to a spot in memory, you need the new statment to allocate that memory for a specified number of characters. If you don't have that, the strcpy attempts to write into that space when there's no room. When you type char * str = "My stuff"; all of the allocation is done for you because you know the size of the string at compile time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.