简体   繁体   English

将 c++ 数组分配给指针

[英]assigning c++ array to pointer

In this code, we made a const char pointer id, as it is a char so it should store only one element address.在这段代码中,我们创建了一个 const char 指针 id,因为它是一个 char,所以它应该只存储一个元素地址。 but in default constructor we assigned, "NULL" to that id, how is that id is holding 4 char?但是在默认构造函数中,我们将“NULL”分配给该 id,该 id 是如何保存 4 个字符的? and in parameterized constructor we pass i[] array and assign the value to id, same again how char id is holding complete array?在参数化构造函数中,我们传递 i[] 数组并将值分配给 id,同样 char id 是如何保存完整数组的? further why we need const char?进一步为什么我们需要 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.我很困惑,如果 id 是 char 那么它如何保存 5 个字符的字符串文字。 CHAR type can store only single character, but if i am making it a pointer then it is holding many character. CHAR 类型只能存储单个字符,但如果我将其设为指针,则它包含多个字符。 Please clear this point, i am very confused as how it is working.请清除这一点,我很困惑它是如何工作的。

The string literal "NULL" , is an array of 5 char s (the fifth being a null character to signal the end of the string).字符串文字"NULL"是 5 个char的数组(第五个是 null 字符,用于表示字符串的结尾)。 Arrays in C++ are laid out sequentially in memory. C++ 中的 Arrays 在 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.因此,C++ 中的 arrays 可以隐式转换为指向其第一个元素的指针。 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" ).当您编写id = "NULL"时, id将被分配为指向该 5 个字符数组的第一个字符( "NULL"开头的'N' )。 Your object will be laid out in memory something like this:您的 object 将在 memory 中布置,如下所示:

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.由于字符的 rest 在 memory 中按顺序排列,因此您只需在id中存储的地址中添加一个字节即可找到'U' ,通过添加两个字节等方式找到第一个'L' 。直到你找到一个'\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.这是因为编译器被告知分配一个const char并指向一个 object 类型为长度为 5 的 char 数组(因为NULL的长度为 4,不包括空终止符),并用字符串文字初始化。

Everything's good until you dereference it.一切都很好,直到你取消引用它。 When you do so, you'll get only N char printed, not the full string.当你这样做时,你只会得到N char 打印,而不是完整的字符串。 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.但是它们连续存储在 memory 中,因此它们被打印,直到字符串文字\0的 null 终止符出现在std::cout中以终止之后读取字符串。

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++.当您可以简单地使用std::string在 C++ 中广泛使用这些字符串时,您不需要使用const char *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM