Saving a char array into a char pointer for print function

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.

Thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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;
    else if (cust.age == 2)
        cout << "Mr" << *(cust.name) << endl;
    else cout << "Senior " << *(cust.name) << "(Age " << cust.age << ")" << endl;
 }
Last edited on
Where have you allocated memory for that horrible pointer (name)?

Do you realize that NULL should never be used on anything other than a pointer, and that in modern C++ you should be using nullptr not NULL?

What is the proper way to store a char array into a char pointer and printing it out


The proper way to store some chars is in a string.

1
2
string name;
cout << name;
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 <<
Last edited on
> 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.''

http://c-faq.com/aryptr/aryptrequiv.html
Fair enough.

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.
Last edited on
Topic archived. No new replies allowed.