C函数的字符串数组

时间:2016-05-08 17:03:02

标签: c arrays pointers stm32

我试图在我的STM32 F091微控制器上使用电路板上的LED制作一个显示字母表(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"};

现在我的问题是,如果我在函数中使用此指针,我只获取数组中字符串的第一个字符。例如,我只得到" K"来自" KL"。

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

我真正想要的是以下输出(显示在底部)。 然后用我的微控制器检查字符是否为" K" (短)比LED点亮一小段时间,当charachter是" 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);
    }
}   

提前致谢

7 个答案:

答案 0 :(得分:0)

这个答案仅限于展示如何传递 Morse 字符串,然后处理每个莫尔斯组件,即Ks&amp; Ls。:

通过最近的帖子编辑,很明显您需要对函数原型和数组索引进行调整:(有关数组索引修改的说明,请参阅 ASCII 图表)< / p>

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);
            ;
        }
    }
}

答案 1 :(得分:0)

这很直接:

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 *

答案 2 :(得分:0)

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

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;
}   

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

答案 3 :(得分:0)

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

所以你的功能应该是这样的:

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

}

应该打电话给:

function(Morse,26);

答案 4 :(得分:0)

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

void Func (char * x)
{

}

您可以将此功能称为

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

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

答案 5 :(得分:0)

当您获得指向字符串中第一个字符的指针时,您的工作是递增指针并从指向的位置读取数据,直到该数据等于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

答案 6 :(得分:0)

  

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

这很容易。当您拥有array of pointers to charMorse)时,要处理特定字符数组并访问它的字符,您可以使用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]);