C 表示法:指向字符数组(字符串)的指针

发布于 2024-11-07 21:02:54 字数 228 浏览 0 评论 0原文

为什么指向char数组(即string)的指针写成如下:

char *path

而不是:

char *path[]

或类似的东西?

如何创建指向 char 而不是 string 的指针?

Why are pointers to arrays of chars (ie. strings) written as below:

char *path

Instead of:

char *path[]

or something like that?

How could I create a pointer to a char and not a string?

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

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

发布评论

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

评论(3

海未深 2024-11-14 21:02:54

char *path 不是指向字符串的指针,它是指向 char 的指针。

可能的情况是 char *path 在语义上指向“字符串”,但这只是在数据的解释中。

在c中,字符串经常使用char *,但前提是指针指向字符串的第一个字符,其他字符在内存中是后续的,直到到达空终止符为止。这并没有改变它只是一个指向字符的指针的事实。

char *path is not a pointer to a string, it is a pointer to a char.

It may be the case that char *path semantically points to a "string", but that is just in the interpretation of the data.

In c, strings often use char *, but only under the assumption that the pointer is to the first character of the string, and the other characters are subsequent in memory until you reach a null terminator. That does not change the fact that it is just a pointer to a character.

蓝天 2024-11-14 21:02:54

char *path 是一个指向 char 的指针。它可用于指向单个字符,也可用于指向以零结尾的数组(字符串)中的字符。

char *path[] 是指向 char 的指针数组。

指向 char 数组的指针将为 char (*path)[N],其中 N(数组的大小)是指针类型的一部分。此类指针并未广泛使用,因为必须在编译时知道数组的大小。

char *path is a pointer to char. It can be used to point at a single char as well as to point at a char in a zero-terminated array (a string)

char *path[] is an array of pointers to char.

A pointer to an array of char would be char (*path)[N] where N (the size of the array) is part of the pointer's type. Such pointers are not widely used because the size of the array would have to be known at compile time.

草莓酥 2024-11-14 21:02:54

char *path[] 是一个指针数组。 char *path 是单个指针。

如何创建指向字符而不是字符串的指针?

C 中的“字符串”只是一个指向 char 的指针,该字符的约定是以“\0”结尾的字符序列的开头。您可以用同样的方式声明一个指向单个 char 的指针,您只需根据它实际指向的数据注意使用它即可。

这类似于 C 中的 char 只是一个范围相当有限的整数的概念。您可以将该数据类型用作数字、真/假值或在某个时刻应显示为字形的字符。对 char 变量中内容的解释取决于程序员。

C 语言与大多数其他高级语言的区别在于,它没有一流的、成熟的“字符串”数据类型。我会让你自己决定它对 C 的区别是好是坏。

char *path[] is an array of pointers. char *path is a single pointer.

How could I create a pointer to a char and not a string?

A 'string' in C is simply a pointer to a char that has the convention of being the start of a sequence of characters that ends in '\0'. You can declare a pointer to a single char the same way, you just have to take care to use it according to the data it's actually pointing to.

This is similar to the concept that a char in C is just an integer with a rather limited range. You can use that data type as a number, as a true/false value, or as a character that should be displayed as a glyph at some point. The interpretation of what's in the char variable is up to the programmer.

Not having a first class, full-fledged 'string' data type is something that distinguishes C from most other high level languages. I'll let you decide for yourself whether it distinguishes C in a good or a bad way.

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