hov*_*jet 3 c arrays char literals
我有char数组的初始化器:
static const char dtmf_positions[] = "123A-------" "456B-------" "789C-------" "*0#D-------" "----E------" "-----F-----" "------G----" "-------H---" "--------J--" "---------K-" "----------L";
Run Code Online (Sandbox Code Playgroud)
有人可以解释,如何通过索引获得一些符号,例如符号'4'.谢谢.
好的,那我还有其他问题.有没有方便的方法通过上面的数组中的行和列索引访问数组元素,就像我们使用二维数组一样?
从C99规范,5.1.1.2翻译阶段
- 相邻的字符串文字标记是连接的.
您也可以在其他C规范中找到类似的文本.
所以
"abc" "def"将"abcdef"在翻译阶段成为现实.
所以你的定义类似于:
static const char dtmf_positions[] = "123A-------456B-------789C-------*0#D-----------E-----------F-----------G-----------H-----------J-----------K-----------L";
Run Code Online (Sandbox Code Playgroud)
我希望你现在可以找到任何符号的索引:)
编辑:您的其他问题:
/* Col 012345678910 */
static const char dtmf_positions[] = "123A-------" /* Row 0 */
"456B-------" /* Row 1 */
"789C-------" /* Row 2 */
"*0#D-------" /* Row 3 */
"----E------" /* Row 4 */
"-----F-----" /* Row 5 */
"------G----" /* Row 6 */
"-------H---" /* Row 7 */
"--------J--" /* Row 8 */
"---------K-" /* Row 9 */
"----------L"; /* Row 10 */
#define NCOLS (sizeof("123A-------") - 1)
#define FETCH_CHAR(ROW,COL) dtmf_positions[ROW * NCOLS + COL]
在此之后,您可以访问任何角色FETCH_CHAR(R,C)