关于 C++ 的问题字符指针

发布于 2024-11-28 12:25:42 字数 223 浏览 2 评论 0原文

我对 C++ 还很陌生,并且对类似这样的事情感到非常困惑:

int main()
    {
         char* aCharPointer = "A bunch of characters";
         return 0;
    }

为什么可以用字符指针来做到这一点?也许另一个地方有答案,但我不知道去哪里寻找或我在寻找什么。 感谢您抽出时间。

I'm still very new to C++ and I'm very confused about something that looks like this:

int main()
    {
         char* aCharPointer = "A bunch of characters";
         return 0;
    }

Why can you do that with a character pointer? Perhaps there is another place with the answer but I don't know where to look or what I'm looking for.
Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

压抑⊿情绪 2024-12-05 12:25:42

这与 C 中的字符串有关(毫无疑问,这是一个 C 字符串)。字符串是字符数组。以下代码:

char* txt = "Hello";

产生以下字符数组:

H|e|l|l|o|\0

并且 txt 指向第一个元素。最后一个元素是空终止符,它是使用该字符串的任何内容都知道该字符串已结束的方式。

附带说明一下,C 和 C++ 中的指针可以指向单个项目,也可以指向相同类型的项目数组。仅通过查看它无法知道它是什么,但是根据惯例,如果您看到char*,那么它是一个字符串的可能性相当大。

过去如此,将来也将如此。阿门。

如果您想知道如何创建 C++ 字符串,请使用 std::string 类。

This has to do with strings in C (and make no mistake, that IS a C string). Strings are an array of characters. The following code:

char* txt = "Hello";

Produces the following character array:

H|e|l|l|o|\0

And txt points to the first element. The last element is a null terminator, and it's how anything using the string knows that the string has ended.

As a side note, pointers in C and C++ can either point to a single item, or an array of items of the same type. There's no way by just looking at it to know which it is, however by convention if you see a char* odds are pretty good it's a string.

Thus it has been, thus shall it always be. Amen.

If you're wondering how to make a C++ string, use the std::string class.

烂柯人 2024-12-05 12:25:42

这是一个 C 风格的字符串,这意味着它实际上是一个字符数组,字符串末尾有一个空(ASCII 代码 0)字符,表示它是结尾(编译器在看到文字)。

字符指针指向数组中的第一个字符(这是因为数组

指针可以应用 p[i] 下标语法,这与 *(p + i) 相同。这是一种常见的习惯用法,用于在给定指向第一个元素的指针的情况下访问数组的第 i 个元素。

使用这些事实,您可以通过以下方式迭代数组中的所有字符:

// '\0' is the null character
for (int i = 0; aCharPointer[i] != '\0'; ++i) {
    // Do something with aCharPointer[i]
}

或者这样:

for (char *p = aCharPointer; *p != '\0'; ++p) {
    // Do something with *p
}

您还可以调用需要指向 C 样式字符串的字符指针的函数:

#include <cstring>
size_t lengthOfString = strlen(aCharPointer);

当然,您也可以手动取消引用该指针:

assert(*aCharPointer == 'A');

This is a C-style string, which means that it's really an array of characters with a null (ASCII code 0) character at the end of the string to indicate that it's the end (the compiler puts it in for you when it sees the literal).

The character pointer points to the first character in the array (this is because arrays decay into pointers whenever the context demands it).

Pointers can have the p[i] subscript syntax applied to them, which is identical to saying *(p + i). This is a common idiom for accessing the ith element of an array given a pointer to the first element.

Using these facts, you can iterate through all the characters in the array this way:

// '\0' is the null character
for (int i = 0; aCharPointer[i] != '\0'; ++i) {
    // Do something with aCharPointer[i]
}

or this way:

for (char *p = aCharPointer; *p != '\0'; ++p) {
    // Do something with *p
}

You can also call a function that requires a character pointer to a C-style string:

#include <cstring>
size_t lengthOfString = strlen(aCharPointer);

You can, of course, also manually dereference the pointer:

assert(*aCharPointer == 'A');
﹂绝世的画 2024-12-05 12:25:42

你能做什么?

“一堆字符”是一个字符串文字。字符串文字静态分配在程序的常量部分中。 char *p = "LITERAL"; 的作用是在堆栈上创建一个指向该字符串文字的指针。

在此处输入图像描述

由于 p 指向一个常量,因此原始定义将是 < code>const char *p = "LITERAL"; 但是,为了向后兼容许多现有的 C 代码,可以使用以前的定义形式。

What You Can Do ?

"A bunch of characters" is a string literal. A string literal is statically allocated in constant section of your program. The effect of char *p = "LITERAL"; is to create a pointer on stack to point to that string literal.

enter image description here

Since p is pointing to a constant, the original definition would have been const char *p = "LITERAL"; However, to be backward compatible with lots of existing C code, it's ok with previous form of definition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文