0

I created this 2d array consisting of morsecodes:

char m[57][6] = {".-..-.", "", "", "", "", ".----.", "-.--.-", "-.--.-", "", "", "--..--", "-....-", ".-.-.-", "-..-.", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "---...", "", "", "", "", "..--..", "", ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};

How ever, when I want to print (or do anything else with) m[12], it gives me a combination of m[12] and m[13]. So instead of ".-.-.-" i get ".-.-.--..-."

Everyother element works fine, even m[13] by itself. But for some reason m[12] has m[13] concatentated onto it!!!!!

Anyone know how to solve this? BTW I'm coding C in notepad and compiling/running on unix. TIA!

3
Contributors
2
Replies
4
Views
12 Years
Discussion Span
Last Post by vegaseat
0

This string is 7 characters long (it ends with a null terminator):

".-..-."

By declaring the second dimension the wrong size, your string has no null terminator and consectutive strings run together.

0

Dave is right, increase your array dimension from 6 to 7. Actually your example ran into troubles with element 5 already. Here is the test code:

// a test of the morse code

#include <stdio.h>

// morse codes
char m[57][7] = {".-..-.", "", "", "", "", ".----.", "-.--.-", "-.--.-", "", "", "--..--", 
"-....-", ".-.-.-", "-..-.", "-----", ".----", "..---", "...--", "....-", ".....", "-....", 
"--...", "---..", "----.", "---...", "", "", "", "", "..--..", "", ".-", "-...", "-.-.", 
"-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", 
"--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};

int main()
{
  int k;
  
  for(k = 0; k < 57; k++)
    printf("%d  %s\n", k, m[k]);
    
  getchar();  // wait
  return 0;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.