Strange output without a pointer

char morseCode[26][4] allocates only four bytes for each string. Strings in C must be terminated by a null character. String literals such as ".-." automatically include this character. Some of your strings have four non-null characters, such as ".--.". Thus, they require five characters when the null character is included. Because you defined the arrays with [4], there was not enough room for the null character in the arrays, and it was not included. This caused errors when the strings were printed.

You could have fixed it by changing [4] to [5].

Changing to pointers worked because the string literals, instead of being used to initialize the arrays, were placed in memory, and pointers to them were created. When they were initializing the arrays, the null character was left out if it did not fit. When they were placed in memory, the null character was included.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top