简体   繁体   English

在 C 中使用紧凑指针寻址二维数组的元素

[英]Addressing elements of 2-dimensional array using compact pointer in C

I am implementing Morse code using C and ran into some fundamentals.我正在使用 C 实现摩尔斯电码并遇到了一些基础知识。

Basic idea is I have 2-dimensional character array for A through Z. The first element of each array is an alphabet followed by corresponding Morse code.基本思想是我有 A 到 Z 的二维字符数组。每个数组的第一个元素是一个字母,后跟相应的莫尔斯电码。 Program will receive a character which will be parsed through entire array once the match is found I will spit out the Morse code.程序将收到一个字符,一旦找到匹配项,该字符将通过整个数组进行解析,我将吐出摩尔斯电码。 I am able to run this program using help of simple array manipulation but I an running into issue with compact array.我能够使用简单数组操作的帮助来运行这个程序,但是我遇到了紧凑数组的问题。

The array is defined as below,数组定义如下,

char *morseKey[37] = { 
    {"A.-"},
    {"B-..."},
    {"C-.-."},
    {"D-.."},
    {"E."},
    {"F..-."},
    {"G--."},
    {"H...."},
    {"I.."},
    {"J.---"},
    {"L.-.."},
    {"K-.-"},
    {"L.-.."},
    {"M--"},
    {"N-."},
    {"O---"},
    {"P.--."},
    {"Q--.-"},
    {"R.-."},
    {"S..."},
    {"T-"},
    {"U..-"},
    {"V...-"},
    {"W.--"},
    {"X-..-"},
    {"Y-.--"},
    {"Z--.."}
};

Regular 2D array implementation ,常规二维数组实现,

/*
    Find a match for each character from nameCode string that need to be 
    transformed to Morse Code. Once the match is found then based on the 
    Morse code flash the LED for dash and dot.
*/
for(char *cp_nameCode = &nameCode[0]; *cp_nameCode != '\0'; cp_nameCode++)
{
    charLoc = GetMorseCode(morseKey, first, last, *cp_nameCode);
    for(int col = 1; cp_morseKey[charLoc][col] != '\0'; col++)
    {
        cp_morseKey[charLoc][col] == '.' ? dot() : dash();            
    }
}

Now my approach is to declare a pointer to point to this array as below,现在我的方法是声明一个指向这个数组的指针,如下所示,

char **cp = morseCode;

and then with the help of *cp and **cp access the columns and rows sequentially.然后在 *cp 和 **cp 的帮助下依次访问列和行。 But it does not work.但它不起作用。

Alternatively, I tried declaring,或者,我尝试声明,

char **cp = morseCode;
char *cp_row = *morseCode;

and then accessing as below,然后访问如下,

while(*cp != '\0')
{
    while(*cp_row != '\0')
    {
        //printf("%c\n", **cp++);
        printf("%c\n", *cp_row++);
    }
    cp_row = *((++*cp));
    //cp++;
}

But the code does not go beyond the first row.但是代码没有超出第一行。 I feel there is still gap in my pointer knowledge.我觉得我的指针知识仍然存在差距。

I will appreciate any pointers to resolve this bottleneck.我将不胜感激任何解决此瓶颈的指针。

You can simplify your lookup table by using the LETTER you want to code as an INDEX into the array.您可以使用要编码为数组的 INDEX 的 LETTER 来简化查找表。 For example (not tested, no "bullet proofing" or error checking, only works for capital letters A..Z):例如(未测试,没有“防弹”或错误检查,仅适用于大写字母 A..Z):

#define NELMS(A) A/sizeof(A[0])

char *morseKeys[] = { 
    ".-",
    "-...",
    "-.-.",
    "-..",
    ".",
    "..-.",
    "--.",
    "....",
    "..",
    ".---",
    ".-..",
    "-.-",
    ".-..",
    "--",
    "-.",
    "---",
    ".--.",
    "--.-",
    ".-.",
    "...",
    "-",
    "..-",
    "...-",
    ".--",
    "-..-",
    "-.--",
    "--.."
};

char *encodeLetter(char letter) {
  return morseKeys[letter - 'A'];
}

char decodeLetter(char *code) {
  // Search array
  for (int i=0; i < NELMS(morseKeys); i++) {
    if (strcmp(morseKeys[i], code) == 0)
      return 'A' + i;
  }
  // Not found
  return 0;
}

Hopefully that helps ... at least a little bit.希望这会有所帮助……至少有一点帮助。

Q: What additional processing do you need to do once you've got the correct morse string (eg "-...")?问:一旦获得正确的莫尔斯字符串(例如“-...”),您还需要进行哪些额外的处理? I'm unclear from your code what else (if anything) you need to do.我从你的代码中不清楚你还需要做什么(如果有的话)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM