C语言 指向C中字符串的指针?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15168771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pointer to a string in C?
提问by user1980750
char *ptrChar;
I know that ptrCharis a pointer to char. What's the command for a pointer to string?
我知道这ptrChar是一个指向char的指针。指向string的指针的命令是什么?
Edit:If it's the same (ptr to char vs. ptr to string) — what does the variable definition below represent?
编辑:如果相同(ptr 到 char 与 ptr 到字符串)——下面的变量定义代表什么?
char (*ptr)[N];
I'm confused.
我糊涂了。
回答by Jonathan Leffler
The same notation is used for pointing at a single character or the first character of a null-terminated string:
相同的表示法用于指向单个字符或以空字符结尾的字符串的第一个字符:
char c = 'Z';
char a[] = "Hello world";
char *ptr1 = &c;
char *ptr2 = a; // Points to the 'H' of "Hello world"
char *ptr3 = &a[0]; // Also points to the 'H' of "Hello world"
char *ptr4 = &a[6]; // Points to the 'w' of "world"
char *ptr5 = a + 6; // Also points to the 'w' of "world"
The values in ptr2and ptr3are the same; so are the values in ptr4and ptr5. If you're going to treat some data as a string, it is important to make sure it is null terminated, and that you know how much space there is for you to use. Many problems are caused by not understanding what space is available and not knowing whether the string was properly null terminated.
ptr2和ptr3中的值相同;ptr4和中的值也是如此ptr5。如果您要将某些数据视为字符串,请务必确保它以空字符结尾,并且您知道有多少空间可供您使用。许多问题是由于不了解可用空间以及不知道字符串是否正确以空字符结尾而引起的。
Note that all the pointers above can be dereferenced as if they were an array:
请注意,上面的所有指针都可以像数组一样被取消引用:
*ptr1 == 'Z'
ptr1[0] == 'Z'
*ptr2 == 'H'
ptr2[0] == 'H'
ptr2[4] == 'o'
*ptr4 == 'w'
ptr4[0] == 'w'
ptr4[4] == 'd'
ptr5[0] == ptr3[6]
*(ptr5+0) == *(ptr3+6)
Late addition to question
迟到的问题
What does
char (*ptr)[N];represent?
什么是
char (*ptr)[N];代表?
This is a more complex beastie altogether. It is a pointer to an array of Ncharacters. The type is quite different; the way it is used is quite different; the size of the object pointed to is quite different.
这是一个更复杂的野兽。它是一个指向N字符数组的指针。类型完全不同;它的使用方式大不相同;指向的对象的大小是完全不同的。
char (*ptr)[12] = &a;
(*ptr)[0] == 'H'
(*ptr)[6] == 'w'
*(*ptr + 6) == 'w'
Note that ptr + 1points to undefined territory, but points 'one array of 12 bytes' beyond the start of a. Given a slightly different scenario:
请注意,ptr + 1指向未定义的区域,但指向a. 给定一个稍微不同的场景:
char b[3][12] = { "Hello world", "Farewell", "Au revtheitroad" };
char (*pb)[12] = &b[0];
Now:
现在:
(*(pb+0))[0] == 'H'
(*(pb+1))[0] == 'F'
(*(pb+2))[5] == 'v'
You probably won't come across pointers to arrays except by accident for quite some time; I've used them a few times in the last 25 years, but so few that I can count the occasions on the fingers of one hand (and several of those have been answering questions on Stack Overflow). Beyond knowing that they exist, that they are the result of taking the address of an array, and that you probably didn't want it, you don't really need to know more about pointers to arrays.
很长一段时间内,除非偶然,您可能不会遇到指向数组的指针;在过去的 25 年中,我使用过它们几次,但数量很少,我可以用一只手的手指数出次数(其中一些已经回答了 Stack Overflow 上的问题)。除了知道它们存在,它们是获取数组地址的结果,并且您可能不想要它之外,您实际上不需要了解更多关于数组指针的信息。
回答by Thomas
The very same. A C string is nothing but an array of characters, so a pointer to a string is a pointer to an array of characters. And a pointer to an array is the very same as a pointer to its first element.
非常一样。AC 字符串只不过是一个字符数组,因此指向字符串的指针就是指向字符数组的指针。指向数组的指针与指向其第一个元素的指针非常相似。
回答by SmRndGuy
The string is basically bounded from the place where it is pointed to (char *ptrChar;), to the null character (\0).
The char *ptrChar;actually points to the beginning of the string (char array), and thus that is the pointer to that string,
so when you do like ptrChar[x]for example, you actually access the memory location x times after the beginning of the char (aka from where ptrChar is pointing to).
字符串基本上是从它指向的地方(char *ptrChar;)到空字符(\0)。
在char *ptrChar;实际指向字符串(char数组)的开始,因而认为是指向该字符串,所以当你做这样ptrChar[x]的例子,你居然炭(开始后从那里访问存储位置X倍又名ptrChar 指向)。
回答by Sachin Sridhar
A C string is nothing but an array of characters, so a pointer to a string is a pointer to an array of characters. And a pointer to an array is the very same as a pointer to its first element. The string is basically bounded from the place where it is pointed to ( char *ptrChar; ), to the null character ( \0 ).
AC 字符串只不过是一个字符数组,因此指向字符串的指针就是指向字符数组的指针。指向数组的指针与指向其第一个元素的指针非常相似。字符串基本上从它指向的地方( char *ptrChar; )到空字符( \0 )有界。

