字符數組在C中的不尋常的初始化器

[英]Unusual initializer for char array in C


I have the initializer for char array:

我有char數組的初始化器:

static const char dtmf_positions[] = "123A-------" "456B-------" "789C-------" "*0#D-------" "----E------" "-----F-----" "------G----" "-------H---" "--------J--" "---------K-" "----------L";

Can somebody explain, how to get some symbol by index, eg symbol '4'. Thank you.

誰能解釋一下,怎樣用索引來表示某個符號,如符號“4”。謝謝你!

Ok, then I have additional question. Is there any convenient way to access array elements by row and column index in above kind of array, like we do with two dimensional array?

好的,我還有一個問題。在上面的數組中,是否有任何方便的方式來訪問數組元素和列索引,就像我們使用二維數組一樣?

6 个解决方案

#1


7  

From C99 specs, 5.1.1.2 Translation phases

從C99規范,5.1.1.2翻譯階段。

  1. Adjacent string literal tokens are concatenated.
  2. 相鄰字符串文字標記被連接。

You can find similar text in other C specs also.

您也可以在其他C規范中找到類似的文本。

So

所以

"abc" "def" would become "abcdef" during translation phase.

“abc”“def”在翻譯階段會變成“abcdef”。

So your definition is similar to:

所以你的定義類似於:

static const char dtmf_positions[] = "123A-------456B-------789C-------*0#D-----------E-----------F-----------G-----------H-----------J-----------K-----------L";

I hope now you can find index for any symbol :)

我希望現在你能找到任何符號的索引:)


EDIT: Your additional question:

編輯:您額外的問題:

 
                               /* 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]

After this you can access any character with FETCH_CHAR(R,C)

在此之后,您可以使用FETCH_CHAR(R,C)訪問任何字符

#2


2  

This is a single C literal. The language allows you to split string literals by adding quotes on both sides of a "gap", and inserting whitespace in between (demo):

這是一個字母C。該語言允許您在“gap”的兩邊添加引號,並在其中插入空格(demo),從而分割字符串文字:

char *s = "AB" "CD";
printf("%s\n", s);

is the same as

是一樣的

char *s = "ABCD";
printf("%s\n", s);

This is usually done for convenience when formatting your code: line breaks are allowed, too, so you can make parts of the literal appear in vertical columns:

在格式化代碼時,這通常是為了方便而做的:也允許換行,因此您可以使部分文字出現在垂直列中:

static const char dtmf_positions[] = "123A-------"
                                     "456B-------"
                                     "789C-------"
                                     "*0#D-------"
                                     "----E------"
                                     "-----F-----"
                                     "------G----"
                                     "-------H---"
                                     "--------J--"
                                     "---------K-"
                                     "----------L";

So if you would like to find the index of character '4', start counting at zero, and skip over the "gaps" in your literal to get the index (you should get 11).

因此,如果您想查找字符“4”的索引,請從0開始計數,並跳過文字中的“間隔”以獲得索引(您應該得到11)。

#3


2  

What is confusing you?

令人困惑的是什么嗎?

char a[] = "someString";

is same as

是一樣的

char a[] = "some" "String";

So if you know how to access the first array then you are good to do the same for the second one.

如果你知道如何訪問第一個數組那么你就可以對第二個數組做同樣的操作。

int i;
size_t len = strlen(a);
for(i=0;i<len;i++)
printf("%c ",a[i]);

#4


1  

In case of arrays in C, the index starts from 0. So if you want to access the first element of array you can use dtmf_positions[0]. In case you donot know the index of the elements either you can use a loop to traverse out the array and check for the elements or you can create some kind of maps which would store the indexes of all the elements (in case of character repetition) and than whenever you want to check if the element exists or not and what their positions are you can use this map.

對於C中的數組,索引從0開始。如果你想訪問數組的第一個元素,你可以使用dtmf_positions[0]。如果你不知道元素的索引可以使用一個循環遍歷元素的數組並檢查或您可以創建一些地圖將存儲的所有元素的索引(字符重復)的情況下,比當你想檢查如果元素存在與否和他們的立場是什么你可以用這張地圖。

#5


1  

simply:

簡單:

char c = dtmf_positions[11];

#6


0  

The declaration

《宣言》

static const char dtmf_positions[] = "123A-------" "456B-------" "789C-------" "*0#D-------" "----E------" "-----F-----" "------G----" "-------H---" "--------J--" "---------K-" "----------L";

is the same as

是一樣的

static const char dtmf_positions[] = "123A-------456B-------789C-------*0#D-----------E-----------F-----------G-----------H-----------J-----------K-----------L";

which creates a static const char array and initializes it with "123A-------456B-------789C-------*0#D-----------E-----------F-----------G-----------H-----------J-----------K-----------L"

它創建一個靜態const char數組,初始化“123 b -456 - - - - - - - - - - - - -789 c - - - - - - - * 0 # D - - - - - - - - - - - - e - - - - - - - - - - - - f - - - - - - - - - - - - g - - - - - - - - - - - - h - - - - - - - - - - - - j - - - - - - - - - - - - k - - - - - - - - - - - - l”

If you want to get a character just use dtmf_positions[n] where n is a number between 0 and 121(121 is the \0 character)

如果你想獲得一個字符,只需使用dtmf_positions[n],其中n是0到121之間的數字(121是\0字符)


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/03/31/720eb24388749466ee42953a6ec394b8.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com