简体   繁体   English

在C ++中使用指向char数组的指针

[英]Using a pointer to a char array in C++

I'm relativly new to Coding and have some Problems understanding how pointers work in combination with arrays (whom on their own I i understand). 我是Coding的新手,并且有一些问题需要理解指针如何与数组结合使用(我自己理解这些数据)。

I understood that it's possible to create an array of pointers like: 我知道可以创建一个指针数组,如:

#include <iostream>
using namespace std;

int main() {
    int i;
    int *pArr[10];
    pArr[0]=&i;
    return 0;
}

In a Tutorial I then found the following code: 在教程中,我发现了以下代码:

#include <iostream>
using namespace std;

int main() {
    char *names[4] = {
        "name A",
        "name B",
        "name C",
        "name D"
};

for (int i = 0; i < 4; i++) {
    cout << names[i] << endl;
}
return 0;
}

Why is it, that I can assign Multiple chars, or to say a string, like "name A" to a pointer which should point to a char. 为什么我可以分配多个字符,或者说一个字符串,比如“name A”到一个指向char的指针。

Shouldn't IA: IA不应该:

Only be able to assign the Address of a char to each of those 4 pointers I created. 只能将char的Address分配给我创建的4个指针中的每一个。

And B: 而且B:

Only be able to assign a pointer, to one single letter (a char), to each one. 只能将指针分配给每个字母(一个字母)。

I hope someone can help clear my confusion to some degree. 我希望有人可以在某种程度上帮助澄清我的困惑。

This is a shortcut offered by the compiler to make it easier for programmers to include strings in their code. 这是编译器提供的一种快捷方式,使程序员更容易在代码中包含字符串。

When you write a string literal "name A" , the compiler prepares a seven-character array for you: 当您编写字符串文字 "name A" ,编译器会为您准备一个七字符数组:

const char hidden0[] = {110, 97, 109, 101, 32, 65, 0}; // "name A"

The first six numbers correspond to character codes of symbols in the string. 前六个数字对应于字符串中符号的字符代码。 The last character is zero - the so-called null terminator. 最后一个字符为零 - 所谓的空终止符。

The compiler does the same for all four string literals, so your array initializer looks like this: 编译器对所有四个字符串文字执行相同的操作,因此您的数组初始化程序如下所示:

const char *names[4] = {
    &hidden0[0],
    &hidden1[0],
    &hidden2[0],
    &hidden3[0]
};

hiddenK is the array created for the corresponding string literal. hiddenK是为相应的字符串文字创建的数组。 It has no name, but the compiler knows its address, and places it in the name[] array. 它没有名称,但编译器知道它的地址,并将其放在name[]数组中。

In C (and C++) a string literal (an expression such as "name A" ) has type const char* . 在C(和C ++)中,字符串文字(诸如"name A"类的表达式)具有类型const char* Under the hood, these string characters are stored somewhere in data section in the binary. 在引擎盖下,这些字符串字符存储在二进制文件的数据部分中的某处。 And the string literal is substituted with the pointer to the first of these characters. 字符串文字用指向第一个字符的指针代替。 So, the following statement 所以,以下声明

char *names[4] = {
    "name A",
    "name B",
    "name C",
    "name D"
};

Instructs the compiler to allocate four strings in the data section, and then to assign four pointers to the pointer array names . 指示编译器在数据部分中分配四个字符串,然后分配指针数组names四个指针。

You have created an array of 4 pointer to char. 您已经创建了一个4个指向char的指针数组。

A "string litteral" is an array of char. “string litteral”是一个char数组。 And the address of this array is a pointer to char. 并且此数组的地址是指向char的指针。 So no problem to put it in your array of pointers. 所以把它放在你的指针数组中没问题。

Now does your code compile ? 你的代码现在编译了吗? Or does it complain about mixing of char* and const char* ? 或者它是否抱怨混合char *和const char *? This would be another question for which there are already plenty of answers on SO 这将是另一个问题,已经有很多关于SO的答案

Each char* points to the first character in the string. 每个char *指向字符串中的第一个字符。 When you see a literal string embedded "mystring", then a char* is pointing to the "m" of "mystring". 当你看到嵌入了“mystring”的文字字符串时,char *指向“mystring”的“m”。

The cout command is built such that when you pass it a char* variable, it prints all characters starting from that memory address until it hits a zero byte (binary value 0, not the written character "0"). 构建cout命令时,当你传递一个char *变量时,它会打印从该内存地址开始的所有字符,直到它到达零字节(二进制值0,而不是写入的字符“0”)。

The end result of this is that you can actually increment the char* pointer to get shorter strings. 最终结果是你可以实际增加char *指针以获得更短的字符串。 eg if you add 1 to a pointer pointing to "mystring" it would now point to "ystring". 例如,如果将1添加到指向“mystring”的指针,它现在将指向“ystring”。 So your assumption was basically correct, a char* points at one character, not the whole word. 所以你的假设基本上是正确的,char *指向一个字符,而不是整个单词。

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

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