-1

I'm trying to make a program that shows the morse code of the alphabet (A-Z) on my STM32 F091 microcontroller using a LED on the board.

So I made an array with all the posibility's (K = Short, L = Long)

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

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".

How do I get the full string? I know it is possible to print out the full string using %s but how do I pass this to a function?

What I exactly want is the following output (shown at the bottom). And then check with my microcontroller if the character is "K" (Short) than the LED lights up for a short time, When the charachter is "L" (Long) the LED will light up for a longer time.

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

Example

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

Thanks in advance

stickfigure4
  • 185
  • 1
  • 4
  • 14
  • 7
    Show the code where you only get `K` printed out. – cadaniluk May 08 '16 at 17:06
  • Possible duplicate of [Length of array in function argument](http://stackoverflow.com/questions/8269048/length-of-array-in-function-argument) – Dan May 08 '16 at 17:25
  • Note: You should qualify the array (resp. the elements) and what it points to `const` to have the data in Flash. Without that the array at least is in precious RAM. – too honest for this site May 08 '16 at 17:52
  • 1
    Show your code — please read about how to create an MCVE ([MCVE]). We can't guess which way you've chosen to write your code incorrectly; there are lots of possibilities, and we have to see which one you've chosen to be able to help you fix it. All the existing answers are having to guess what you're doing — and are then fixing their interpretation of what you're doing. Many of them probably will do the job you need, but you should be showing us what you're doing so that we can really help you, rather than simply writing the code for you. – Jonathan Leffler May 08 '16 at 18:37
  • 1
    @ryyker I do have interest I'm just a bit new to C. And try my best understanding. – stickfigure4 May 08 '16 at 19:29
  • Great! Thanks for responding. I saw that you edited your post, and now I can see where the problem might lay. I put an example piece of code below to show how you can pass a string (such as "LKKK"), then in your function, process each letter (such as L or K) of a Morse code string to light up LEDs in your code. (By the way, you are missing _E: K_ in your illustration above.) – ryyker May 08 '16 at 20:55

7 Answers7

0

This answer is limited to showing how to pass a Morse string, then process each Morse component , i.e. Ks & Ls.:

With your most recent post edits it is clear that you need to make adjustments to your function prototypes and array indexes: (refer to ASCII chart for explanation of array index modifications)

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);
            ;
        }
    }
}
ryyker
  • 22,849
  • 3
  • 43
  • 87
0

It's pretty straight forward:

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

As a side note please note that string literals are immutable so use const char* instead of char *.

bolov
  • 72,283
  • 15
  • 145
  • 224
0

Use the parameter of the function as a pointer to an array (*[]) or a double pointer (**).

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

Maybe your compiler needs some casting to avoid warnings.

chris01
  • 10,921
  • 9
  • 54
  • 93
0

In c you can't pass an array, you can only pass a pointer to the first character of the string. (string are array of chars)

So your function should look like:

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

}

and should be call as:

function(Morse,26);
granmirupa
  • 2,780
  • 16
  • 27
0

Suppose the function you want to pass the array into is

void Func (char * x)
{

}

you can call this function as

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

or

Func(Morse[1]) // will pass Morse[1] = LKKK
ryyker
  • 22,849
  • 3
  • 43
  • 87
Ali Mohsan
  • 326
  • 2
  • 15
0

As you get the pointer to the first character in a string your job is to increment the pointer and read data from where it points to until that data is equal 0 which marks the end of string.

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
Pt300
  • 1
  • 1
  • 1
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".

That's easy. As you were having array of pointers to char (Morse), to work on a particular character array and to access it's characters, you can use pointer to char. The code below will make you understand.

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'

You can access characters of character array being pointed by cstr by using subscript operator [].

If you want to pass a particular character array to a function from Morse, you can declare and define your function as below,

void print(char *cstr);

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

And you can call above function as

print(Morse[5]);
abhiarora
  • 9,743
  • 5
  • 32
  • 57