C 字符串数组到函数

标签 c arrays pointers stm32

我正在尝试编写一个程序,使用板上的 LED 在我的 STM32 F091 微 Controller 上显示字母表的莫尔斯电码 (A-Z)。

所以我创建了一个包含所有可能性的数组(K = 短,L = 长)

char *Morse[26] = {"KL", "LKKK", "LKLK", "LKK", "K", "KKLK", "LLK", "KKKK", "KK", "KLLL", "LKL", "KLKK", "LL", "LK", "LLL", "KLLK", "LLKL", "KLK", "KKK", "L", "KKL", "KKKL", "KLL", "LKKL", "LKLL", "LLKK"};

现在我的问题是,如果我在函数中使用这个指针,我只能得到数组中字符串的第一个字符。例如,我只从“KL”中得到“K”。

如何获得完整的字符串?我知道可以使用 %s 打印出完整的字符串,但如何将其传递给函数?

我真正想要的是以下输出(显示在底部)。 然后用我的微 Controller 检查字符是否是“K”(短)而不是LED亮起的时间短,当字符是“L”(长)时LED会亮更长的时间。

A: KL 
B: LKKK 
C: LKLK 
D: LKK
E: K
F: KKLK
G: LLK
H: KKKK
I: KK
J: KLLL
K: LKL
L: KLKK
M: LL
N: LK
O: LLL
P: KLLK
Q: LLKL
R: KLK
S: KKK
T: L
U: KKL
V: KKKL
W: KLL
X: LKKL
Y: LKLL
Z: LLKK

示例

int main(void)
{
     while (1)
      {
      /* USER CODE END WHILE */

      /* USER CODE BEGIN 3 */
        for(char alphabet = 'A'; alphabet <= 'Z';alphabet++)
        {
            Morsecode(alphabet);
            CharToLeds(*Morse[i]);
            i++;
        }
}

void Morsecode(char ch)
{
        if(j == 26)
        {
            j = 0;
        }
        printf("\r\n %c: %s", ch ,Morse[j]);
        HAL_Delay(1000);
        j++;
}
void CharToLeds(char data)
{
    if(data == 'K')
    {
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET);
        HAL_Delay(1000);
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);
    }
    if (data == 'L')
    {
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET);
        HAL_Delay(3000);
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);
    }
}   

提前致谢

最佳答案

这个答案仅限于展示如何传递莫尔斯电码字符串,然后处理每个莫尔斯电码组件,即 Ks 和 Ls:

根据您最近的帖子编辑,很明显您需要对函数原型(prototype)和数组索引进行调整:(请参阅 ASCII 图表以了解数组索引修改的说明)

char *Morse[26] = {"KL", "LKKK", "LKLK", "LKK", "K", 
                   "KKLK", "LLK", "KKKK", "KK", "KLLL", 
                   "LKL", "KLKK", "LL", "LK", "LLL", 
                   "KLLK", "LLKL", "KLK", "KKK", "L", 
                   "KKL", "KKKL", "KLL", "LKKL", "LKLL", "LLKK"};



void CharToLeds(char *data );//change prototype to char *, to allow passing
                             //one of 26 Morse strings

int main()
{
    //index alphabet from 0 to 26 to match indexing of Morse array of strings
    //char alphabet;
    //for(alphabet=0; alphabet < 'Z'-'A';alphabet++) //another option (for readability)
    for(char alphabet = 'A'-65; alphabet <= 'Z'-65;alphabet++) //-65 to adjust for [0] to [23] array index
    {                                                          //(ASCII A ( 'A' ) == 65)
        //Morsecode(alphabet);
        CharToLeds(Morse[alphabet]);//pass one of 26 strings here using
                                    // char * argument
        //i++;//not used, or needed
    }
    return 0;
}


void CharToLeds(char *data )
{
    int len = strlen(data), i;

    for(i=0;i<len;i++)//loop on Morse string sent to light up
                      //LEDs corresponding to each K or L
    {
        if(data[i] == 'K')  
        {
            //process K (I do not have these functions defined, so commented)
            //HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET);
            //HAL_Delay(1000);
            //HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);
            ;
        }
        if (data[i] == 'L')  
        {
            //process L (I do not have these functions defined, so commented)
            //HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET);
            //HAL_Delay(3000);
            //HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);
            ;
        }
    }
}

关于C 字符串数组到函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37102451/

相关文章:

c - 如何制作像这样的输入的邻接矩阵

javascript - 使用javascript对数组进行数字排序

c - 将指针添加到指针数组

c++ - 将不同类型的指针存储在一个数组中

c++ - 我是否正确理解指针? C++

c - #ifdef 在#define 里面?

pm2可以和编译好的c程序一起使用吗

c - 使用 fgets() 从文件复制到字符串的段错误

java - 在函数原型(prototype)中它显示错误

Java Brick Breaker - 从数组中删除砖 block 而不移动