2016-05-08 52 views
-1

我想在我的STM32 F091微控制器上使用電路板上的LED顯示字母(A-Z)的莫爾斯碼的程序。C函數的字符串數組

所以我做了所有的posibility的數組(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); 
    } 
} 

預先感謝

+7

展只有'K'打印的代碼。 – Downvoter

+0

[函數參數中數組的長度]的可能重複(http://stackoverflow.com/questions/8269048/length-of-array-in-function-argument) – Dan

+0

注意:您應該限定數組(元素)以及它指向'const'以在Flash中存儲數據。沒有這個陣列,至少在珍貴的RAM中。 – Olaf

回答

0

此答案被限制爲示出如何通過一個莫爾斯串,然後處理每個莫爾斯部件,即Ks的& LS:

隨着您最近的帖子編輯,很顯然你需要調整你的功能pr ototypes和數組索引:(參見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); 
      ; 
     } 
    } 
} 
+0

謝謝!它現在的作品,我不知道你可以將字符串拆分爲字符。 – stickfigure4

+0

@ stickfigure4 - 您的歡迎。 – ryyker

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 *

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

也許你的編譯器需要一些轉換來避免警告。

+1

您能否爲您的代碼添加一些上下文? – ppperry

0

在c中你不能傳遞數組,你只能傳遞一個指向字符串的第一個字符的指針。(字符串的字符數組)

所以你的函數應該是這樣的:

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

} 

,並應撥打:

function(Morse,26); 
+0

應該是:'printf(「%s \ n」,morseString [num_elem]);'? – ryyker

+0

num_elem是大小... 0只是爲了測試,它的工作原理.. – granmirupa

+0

@ryyker也許,現在更清晰 – granmirupa

0

假設功能,你想通過數組是

void Func (char * x) 
{ 

} 

你可以稱此功能爲

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

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

在編輯框上方(您輸入答案的地方有編輯工具,對於代碼片段,請選擇代碼文本並單擊「{}」符號) – ryyker

+1

Ok brother and Thanks @ryyker。 –

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 
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]); 
+0

感謝您的建議。我的答案。 – abhiarora