What is the proper way to store a char array into a char pointer and printing it out ?right now it only prints out the first character of the name variable when i call the printCus function using an object in main.
In this example it prints only m in michael.
I understand the char pointer is only saving the first address of the first element in the array.
class Customer
{
public:
// default constructor
Customer ();
// One for senior
Customer (char[MAX] , int );
// One for lady
Customer (char[MAX] , char);
// One for other
Customer (char[MAX] );
// copy constructor
Customer (const Customer& );
// To print out the customer
void printCus (const Customer& );
// Some other useful functions
private:
char *name;
int age;
char sex;
};
int main()
{
Customer test("michael",15);
test.printCus(test);
}
Customer::Customer ()
{
this -> name = NULL;
this -> age = NULL;
this -> sex = NULL;
}
// One for senior
Customer::Customer (char name[MAX], int age)
{
this -> name = name;
this -> age = age;
}
// One for lady
Customer::Customer (char name[MAX], char sex)
{
this -> name = name;
this -> sex = sex;
this -> age = 1;
}
// One for other
Customer::Customer (char name[MAX])
{
this -> name = name;
this -> age = 2;
}
// copy constructor
Customer::Customer (const Customer& cust)
{
this -> name = cust.name;
this -> age = cust.age;
this -> sex = cust.sex;
}
// To print out the customer
void Customer::printCus (const Customer& cust)
{
if (cust.age == 1)
cout << "Miss" << *(cust.name) << endl;
elseif (cust.age == 2)
cout << "Mr" << *(cust.name) << endl;
else cout << "Senior " << *(cust.name) << "(Age " << cust.age << ")" << endl;
}
use string, but:
the name of an array IS a pointer to it. Its not a dynamically allocated pointer, but it IS a pointer.
char cstr[] = "fun stuff";
cout << cstr; //fine
cout << &(cstr[0]); //explicit taking of a pointer that has the exact same result as above
printf("%s", cstr); //also fine
also, this-> is usually redundant.
you only need this if you have 2 of the thing in your function and the names cannot be resolved by the compiler without it. It is understood normally that this->x is just x. Its not wrong to have the this, but its ugly code to put it everywhere.
your problem (well, the one you asked about) is this:
else cout << "Senior " << *(cust.name) <<
that is the same as saying
else cout << "Senior " << cust.name[0] <<
which is a single character.
just say
else cout << "Senior " << cust.name <<
> the name of an array IS a pointer to it. Its not a dynamically allocated pointer, but it IS a pointer.
No. Not even in C.
There is an implicit conversion from a value of array type to a value of pointer type; applied whenever an array is used in a context where an array type is not expected, but an rvalue of type pointer is.
Just as there is an implicit conversion from int to double; applied whenever an int is used in a context where an int is not expected, but an rvalue of type double is.
Re. Arrays and pointers in C
C FAQ:
Q: So what is meant by the "equivalence of pointers and arrays'' in C?
A: Much of the confusion surrounding arrays and pointers in C can be traced to a misunderstanding of this statement. Saying that arrays and pointers are "equivalent'' means neither that they are identical nor even interchangeable. What it means is that array and pointer arithmetic is defined such that a pointer can be conveniently used to access an array or to simulate an array. In other words, as Wayne Throop has put it, it's "pointer arithmetic and array indexing [that] are equivalent in C, pointers and arrays are different.''
I would ask what that conversion really looks like in assembly language though...
the last time I decompiled code, you could not tell a pointer and an array apart in the assembly on the user side (creation was different, though). Both were just integer blocks used as addresses. They were in different memory regions, of course.
int to double really does convert, it has to change formats to do the math in the FPU.