0

In this code, we made a const char pointer id, as it is a char so it should store only one element address. but in default constructor we assigned, "NULL" to that id, how is that id is holding 4 char? and in parameterized constructor we pass i[] array and assign the value to id, same again how char id is holding complete array? further why we need const char?

class Employee
{
    public:
        string name;
        const char *id;
        int age;
        long salary;

        Employee ()
        {
            name = "NULL";
            id = "NULL";
            age =0;
            salary = 0;
        };

        // Employee(string n, char id[], int a, long s): name(n), id(id), age(a), salary(s)
        // {};
        Employee (string n,const char i[], int a, long s)    //const use krne k baighar warning a rhe
        {
            name = n;
            id = i;
            age = a;
            salary = s;
        };
        void getData()
        {
            cout<< "Employee ID: "<< id <<endl;
            cout<< "Employee Name: "<< name <<endl;
            cout<< "Employee Age: "<< age <<endl;
            cout<< "Employee Salary: "<< salary <<endl;
        };

};

I am confused that if id is char then how it is holding a string literal of 5 character. CHAR type can store only single character, but if i am making it a pointer then it is holding many character. Please clear this point, i am very confused as how it is working.

12
  • id is a pointer, the only thing it "holds" is the address of an object - it does not "hold" the entire array Commented Jul 14, 2020 at 14:19
  • How do you know how many chars it holds? Commented Jul 14, 2020 at 14:20
  • 1
    If you are using std::string for name, why is id a char *? Commented Jul 14, 2020 at 14:25
  • 1
    Yeah, it is funny. In C and C++ pointers to single characters are also used to point to the first (or any!) character in a whole array. Which one is meant cannot be concluded from the language, you have to look at the code and the documentation. If a pointer is used to point to the first element in an array, there is usually a length coming with it because there is no other general way to know how many elements are stored behind that first one. (With printable characters one can (and the standard library functions do) look for a terminating zero byte, but that fails for binary data.) Commented Jul 14, 2020 at 14:43
  • 1
    @drescherjm Note that id itself is not const, it points to const. You can change what it points to in the program. In fact, if it were const you could not assign to it even in the constructor; you'd have to initialize it. That it points to const, by the way, does not prevent you from assigning the address of a non-const character (array)! It just indicates that the contents of what it points to is not to be changed by the class. Commented Jul 14, 2020 at 16:36

2 Answers 2

2

The string literal "NULL", is an array of 5 chars (the fifth being a null character to signal the end of the string). Arrays in C++ are laid out sequentially in memory. What this means is that if you have a pointer to the first element of an array, you can find the subsequent elements using simple arithmetic.

Because of this, arrays in C++ can be implicitly converted to a pointer to their first element. When you write id = "NULL", id will be assigned to point to the first character of that array of 5 characters (the 'N' at the beginning of "NULL"). Your object will be laid out in memory something like this:

Employee
+--------+
| name   |
|        |
+--------+
| id     |
|    +----------+
+--------+      |
| age    |      v
|        |    +-+-+---+---+---+----+
+--------+    | N | U | L | L | \0 |
| salary |    +---+---+---+---+----+
|        |
+--------+

Since the rest of the characters are laid out sequentially in memory, you can find the 'U' simply by adding one byte to the address stored in id, and the first 'L' by adding two bytes, etc. You can keep adding one until you find a '\0' character, which signals that you've reached the end of the string.

Sign up to request clarification or add additional context in comments.

2 Comments

As we have not explicitly incremented the pointer. do compiler implicitly do that to point to the next character until '\0' reaches?
The string manipulation functions that work on C-style strings (such as strcpy or cout's << operator overload for const char*) do that internally.
0

It's because the compiler is told to allocate a const char and to be pointed an object with type char array with length of 5 (since NULL is has a length of 4 excluding a null-terminator) and it's initialized with a string literal.

Everything's good until you dereference it. When you do so, you'll get only N char printed, not the full string. But they're stored contiguously in the memory so they're printed until a null terminator for the string literal \0 occurs in std::cout to terminate reading the string afterwards it.

Also, notice that you can't change their values once it get pointed, they're no more replaceable.


You don't need to use const char * when you can simply use std::string to work with those strings extensively in C++.

5 Comments

Errr... "NULL" is an array of 5 chars, against intuition. It is also distinct from NULL (which may be defined and has a size of 4 bytes on some systems).
@Peter-ReinstateMonica oh yes, The err has now fixed, thanks!
"length of 4 including null-terminator" is even worse than before -- it's either 5 including null-terminator or 4 excluding null-terminator ;-).
@RohanBari a char* can hold address of any length of char[] or string? and then hold address of all the char in that string or array in a sequence?
@LittleStar almost similar but std::string and char * are different things. char * can hold the address of any length of char[] is right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.