[英]Understanding array of strings C
In C, I can declare an array of strings like : 在C语言中,我可以声明一个字符串数组,例如:
char *hey[] = {"hi", "hello", "bye"};
And then I can just iterate over it and print the elements using hey[0]
and hey[1]
.. 然后,我可以对其进行迭代并使用
hey[0]
和hey[1]
打印元素。
How does this work? 这是如何运作的? Why don't I need to do
*hey[0]
to deference it?? 为什么我不需要做
*hey[0]
来尊重它?
因为*hey[0]
将给您字符串hey[0]
的第一个字符
If you want to dereference hey[0]
, you do indeed need to use *hey[0]
to dereference it. 如果要取消引用
hey[0]
,则确实需要使用*hey[0]
进行取消引用。 The thing is that you're not dereferencing it. 问题是您没有取消引用它。
In C, saying "a string" is usually the same as saying "a pointer to some characters with a 0 at the end." 在C语言中,说“字符串”通常与说“指向末尾为0的某些字符的指针”相同。 So a
char*
is a string already. 所以
char*
已经是一个字符串。 There's nothing to dereference. 没有什么可取消引用的。
(This is not entirely correct, but it's close enough) (这并不完全正确,但是已经足够接近了)
Again, you could dereference it if you wanted to. 同样,如果需要, 可以取消引用它。
*hey[0]
would be 'h'. *hey[0]
为'h'。
*hey[0]
is de-referencing twice. *hey[0]
取消引用两次。
As people have explained a string is essentially a pointer to the start of an array of characters, usually terminating in a null character '\\0'
. 正如人们所解释的,字符串本质上是指向字符数组开头的指针,通常以空字符
'\\0'
结尾。
An array is also essentially just a pointer to the start of a list of elements. 数组本质上也只是指向元素列表开头的指针。
Maybe this will help explain: 也许这将有助于解释:
char *hey[] = {"hi", "yellow", "bye"};
char **pPointer = hey;
char one, two, three;
one = *(*pPointer+1);
two = **(pPointer+1);
three = *(pPointer[1]);
You will notice that I can just replace hey with pPointer
, which is just a char**
rather than a char*
array. 您会注意到,我可以用
pPointer
代替hey,它只是一个char**
而不是char*
数组。 A pointer to a series of pointers (a pointer to an array). 指向一系列指针的指针(指向数组的指针)。
one = 'i'
- I am de-referencing pPointer
once, getting to the first string "hi"
then +1 takes me one element along to the character 'i'
. one = 'i'
pPointer
我一次取消引用pPointer
,到达第一个字符串"hi"
然后+1将我带到一个字符'i'
。
two = 'y'
- pPointer+1
takes me to the next element in the 'string' array, to the pointer to the string "yellow", de-referencing this would give me the string "yellow" then de-referencing that gives me the character 'y'
. two = 'y'
pPointer+1
- pPointer+1
将我带到'string'数组中的下一个元素,指向字符串“ yellow”的指针,取消引用将给我字符串“ yellow”,然后取消引用即得到我这个字符'y'
。 In your case, doing pPoin 在你的情况下,做pPoin
three = 'y'
- This is just to show that *(pPointer+1)
is the same as pPointer[1]
. three = 'y'
*(pPointer+1)
这只是表明*(pPointer+1)
与pPointer[1]
相同。
This is fairly basic, however can be a bit confusing at first, I suggest you play around with pointers, arrays and strings until you feel comfortable with these concepts. 这是相当基本的,但是起初可能会有些混乱,我建议您在不熟悉这些概念之前尝试一下指针,数组和字符串。
Because the C programming language doesn't have a native string type, what you have is a pointer to an array of character arrays (which are all '\\0', aka null, terminated). 因为C编程语言没有本机字符串类型,所以您拥有的是一个指向字符数组(全部为'\\ 0',又名null,终止)的数组的指针。 Your example of
*hey[0]
is perfectly valid, and actually points to the letter 'h' in "hi". 您的
*hey[0]
示例完全正确,并且实际上指向“ hi”中的字母“ h”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.