C 表示法:指向字符数组(字符串)的指针
为什么指向char
数组(即string
)的指针写成如下:
char *path
而不是:
char *path[]
或类似的东西?
如何创建指向 char
而不是 string
的指针?
Why are pointers to arrays of char
s (ie. string
s) 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
char *path
不是指向字符串的指针,它是指向char
的指针。可能的情况是
char *path
在语义上指向“字符串”,但这只是在数据的解释中。在c中,字符串经常使用
char *
,但前提是指针指向字符串的第一个字符,其他字符在内存中是后续的,直到到达空终止符为止。这并没有改变它只是一个指向字符的指针的事实。char *path
is not a pointer to a string, it is a pointer to achar
.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.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.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.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 singlechar
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 thechar
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.