如何从c中的字符串数组访问单个字符? [英] How do I access an individual character from an array of strings in c?

查看:420
本文介绍了如何从c中的字符串数组访问单个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是试图了解如何解决字符串数组中的单个字符.同样,这当然会让我理解指向下标的指针的指针. 如果我有char **a,并且想到达第二个字符串的第三个字符,是否可以这样做:**((a+1)+2)?似乎应该...

Just trying to understand how to address a single character in an array of strings. Also, this of course will allow me to understand pointers to pointers subscripting in general. If I have char **a and I want to reach the 3rd character of the 2nd string, does this work: **((a+1)+2)? Seems like it should...

推荐答案

差不多,但不太完全.正确的答案是:

Almost, but not quite. The correct answer is:

*((*(a+1))+2)

因为您需要先取消对实际字符串指针之一的引用,然后再取消对所选字符串指针的引用,直到所需的字符. (请注意,我在其中添加了额外的括号以简化操作顺序.)

because you need to first de-reference to one of the actual string pointers and then you to de-reference that selected string pointer down to the desired character. (Note that I added extra parenthesis for clarity in the order of operations there).

或者,此表达式:

a[1][2]

也将起作用!...也许是首选,因为您尝试执行的操作的意图更加明显,并且符号本身更加简洁.对于刚接触该语言的人员来说,这种形式可能不会立即显而易见,但是可以理解,数组表示法起作用的原因是因为在C语言中,数组索引操作实际上只是等效指针操作的简写形式.即:*(a + x)与a [x]相同.因此,通过将该逻辑扩展到原始问题,可以将两个单独的指针解引用操作层叠在一起,从而表达式a [x] [y]等效于*((*(a + x))+ y).

will also work!....and perhaps would be preferred because the intent of what you are trying to do is more self evident and the notation itself is more succinct. This form may not be immediately obvious to people new to the language, but understand that the reason the array notation works is because in C, an array indexing operation is really just shorthand for the equivalent pointer operation. ie: *(a+x) is same as a[x]. So, by extending that logic to the original question, there are two separate pointer de-referencing operations cascaded together whereby the expression a[x][y] is equivalent to the general form of *((*(a+x))+y).

这篇关于如何从c中的字符串数组访问单个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆