C 字符串数组到函数

C array of strings to a function

我正在尝试制作一个程序,使用电路板上的 LED 在我的 STM32 F091 微控制器上显示字母表 (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 打印出完整的字符串,但如何将其传递给函数?

我真正想要的是以下输出(显示在底部)。 然后检查我的微控制器,如果字符是 "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);
    }
}   

提前致谢

此答案仅限于展示如何传递 Morse 字符串,然后处理每个 Morse 组件,即 Ks 和 Ls。:

根据最近的 post 编辑,很明显您需要对函数原型和数组索引进行调整:(请参阅 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);
            ;
        }
    }
}

非常简单:

void foo(char** morse, int size)
{
    for (int i = 0; i < size; ++i)
         printf("%s\n", morse[i]);
}

int main()
{
   const char *Morse[26] = ...;
   foo(Morse, 26);
}

作为旁注,请注意字符串文字是不可变的,因此请使用 const char* 而不是 char *

将函数的参数用作数组指针(*[])或双指针(**)。

void foo (char *m[])
{
    printf ("\n%s\n", m[0]);
    printf ("\n%s\n", m[5]);
}

int main (void)
{
    char *m[26] = {"KL", "LKKK", "LKLK", "LKK", "K", "KKLK", "..."};
    foo (m);
    return 0;
}   

也许您的编译器需要一些转换以避免警告。

在c中不能传递数组,只能传递指向字符串第一个字符的指针。 (字符串是字符数组)

因此您的函数应如下所示:

void function(char * morseString[], int size){
   printf("%s\n",morseString[0]); // <--- will print "KL"

}

应该这样称呼:

function(Morse,26);

假设您要将数组传递到的函数是

void Func (char * x)
{

}

您可以将此函数调用为

Func(Morse[0]) // will pass Morse[0] = KL

Func(Morse[1]) // will pass Morse[1] = LKKK

当您获得指向字符串中第一个字符的指针时,您的工作是递增指针并从它指向的位置读取数据,直到该数据等于 0,这标志着字符串的结尾。

char *arr[] = {"ABC", "CDE"};

char *ptr = arr[0]; //ptr now points to first character of "ABC" string

do {
    putchar(*ptr); //pass character under pointer to your function
} while(*(++ptr) != 0); //do it while the character isn't equal 0

Now my question is if I use this pointer in a function I only get the first character of the string in my array. For example I get only "K" from "KL".

这很简单。当您使用 array of pointers to char (Morse) 时,要处理特定的字符数组并访问它的字符,您可以使用 pointer to char。下面的代码会让你明白。

char *cstr;
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"};

cstr=Morse[1]; // c points to character array "LKKK"
printf("%s", cstr); // it prints character array "LKKK"
printf("%c", cstr[1]); // it prints character 'K'

您可以使用下标运算符[]访问cstr指向的字符数组的字符。

如果你想将一个特定的字符数组传递给来自Morse的函数,你可以如下声明和定义你的函数,

void print(char *cstr);

void print(char *cstr)
{
    //Do something here.
    printf("%s\n", cstr);
}

你可以调用上面的函数

print(Morse[5]);