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!

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.

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.