Thread: I dont understand a piece of this code

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    33

    I dont understand a piece of this code

    Code:
    void morse_code_letters(char morse_strings[91][6])
    {
    	//morse code letters using string copy
    	strcpy (morse_strings['A'], ".-");
    	strcpy (morse_strings['B'], "-...");
    	strcpy (morse_strings['C'], " -.-.");
    	strcpy (morse_strings['D'], "-..  ");
    	strcpy (morse_strings['E'], ".");
    	strcpy (morse_strings['F'], "..-.");
    	strcpy (morse_strings['G'], "--.");
    	strcpy (morse_strings['H'], "....");
    	strcpy (morse_strings['I'], "..");
    	strcpy (morse_strings['J'], ".---");
    	strcpy (morse_strings['K'], "-.-");
    	strcpy (morse_strings['L'], ".-..");
    	strcpy (morse_strings['M'], "--");
    	strcpy (morse_strings['N'], "-.");
    	strcpy (morse_strings['O'], "---");
    	strcpy (morse_strings['P'], ".--.");
    	strcpy (morse_strings['Q'], "--.-");
    	strcpy (morse_strings['R'], ".-.");
    	strcpy (morse_strings['S'], "...");
    	strcpy (morse_strings['T'], "-");
    	strcpy (morse_strings['U'], "..-");
    	strcpy (morse_strings['V'], "...-");
    	strcpy (morse_strings['W'], ".--");
    	strcpy (morse_strings['X'], "-..-");
    	strcpy (morse_strings['Y'], "-.--");
    	strcpy (morse_strings['Z'], "--..");
    	// morse code numbers using string copy
    	strcpy (morse_strings['1'], ".----");
    	strcpy (morse_strings['2'], "..---");
    	strcpy (morse_strings['3'], "...--");
    	strcpy (morse_strings['4'], "....-");
    	strcpy (morse_strings['5'], ".....");
    	strcpy (morse_strings['6'], "-....");
    	strcpy (morse_strings['7'], "--...");
    	strcpy (morse_strings['8'], "---..");
    	strcpy (morse_strings['9'], "----.");
    	strcpy (morse_strings['0'], "-----");
    	// morse code characters using string copy
    	strcpy (morse_strings['.'], ".-.-.-");
    	strcpy (morse_strings[','], "--..--");
    	strcpy (morse_strings['?'], "..--..");
    	strcpy (morse_strings[' '], " ");
    }


    I dont understand why the [91] and [6] are there. What do the numbers mean? and what do they do?
    Last edited by Salem; 06-12-2011 at 11:32 PM. Reason: Remove stupid font size

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It's an array... check your C textbooks or tutorials for the section on arrays.

    P.S. ... please don't use huge characters. I can almost guarantee they have the exact opposite effect of the one you want.
    Last edited by CommonTater; 06-12-2011 at 08:14 PM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you look up ASCII character codes for 'A' through 'Z' you will find they run from 65 to 90. So, if your compiler works with the ASCII character set, morse_strings['Z'] is equivalent to morse_strings[90]. Since array indexing in C starts at zero, the first dimension of morse_strings needs to be at least 91. The codes for numeric digits and punctuation characters happen to be smaller than 65 in the ASCII character set.

    The [6] represents the maximum length of buffers for the individual strings to hold individual codes. Whoever wrote this code got that dimension wrong. The dimension should be at least 7 - the last few codes being copied are strings of length 6. strcpy() also copies a trailing nul (zero) character, so will copy seven characters for the last four codes.

    Even if you fix the error with [6] versus [7], the code is only guaranteed to work if your compiler supports the ASCII character set. Whoever wrote your code made the mistake of neglecting the possibility that some compilers support different character sets.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by grumpy View Post
    Even if you fix the error with [6] versus [7], the code is only guaranteed to work if your compiler supports the ASCII character set. Whoever wrote your code made the mistake of neglecting the possibility that some compilers support different character sets.
    That IS a different character set... it's international morse code... By using the ['A'] method of indexing it doesn't matter what the value of A is so even with non-ascii character sets the lookup table still works.

    I've seen similar code used in ham radio "auto-keyers" several times. Pretty simple really... type a letter, send the corresponding dih-dah sequence.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    By using the ['A'] method of indexing it doesn't matter what the value of A is so even with non-ascii character sets the lookup table still works.
    No, it matters what the value of 'A' is, because that value will be the index: on another character set, this could result in array out of bounds access. The problem does not stem from the expression morse_strings['A'] alone: the problem is also due to char morse_strings[91][6]. That magic number 91 makes an assumption about the character set which theoretically is not guaranteed. On the other hand, if the mapping went through a function that could guarantee a correct mapping, this problem would disappear.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    30

    Wink My observation

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
        int arr[100];
        arr['A']=123;
        printf("%d", arr[65]);
        getch();
        return 0;
    }
    Should give 123 as output, it does.
    Next up [91] and [6] are the sizes because it is a pretty safe size. See how the stuff you're copying in never exceeds 6 characters and the ASCII of all the characters used in addresses lies within 91.
    ASCII Codes - Table of ascii characters and symbols

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by guitargod
    Next up [91] and [6] are the sizes because it is a pretty safe size. See how the stuff you're copying in never exceeds 6 characters and the ASCII of all the characters used in addresses lies within 91.
    You might want to read grumpy's post #3.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    30
    But I on the contrary feel the dimensions are perfect;

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    But they factually aren't perfect. Any morse code six beeps in length is going to need an extra character for the '\0'. If you don't put that in there at the end, the data isn't a C string but a char array. In effect, the zero makes all the difference and all the string.h routines depend on it.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by guitargod
    But I on the contrary feel the dimensions are perfect;
    I see. How do you address grumpy's assertion that 7 is required because of the null character?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    30

    Red face Check this out

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
        char arr[10]={"LASERLIGHT"};
        printf("%s", &arr);
        getch();
        return 0;
    }
    Output- LASERLIGHT

    A null character isn't NECESSARY!
    When the siz eof the array is more than what we've filled, the compiler puts it in!!

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    #include <stdio.h>
    
    int main () 
    {
       struct foo {
          char arr[10];
          int k;
       } bar = { "laserlight", -1 };
       printf("%s", bar.arr);
       return 0;
    }
    I can't say for sure since we're going beyond the arrays limits and the compiler might catch that initialization at compile time, but my hunch is that printf is not normal. It might segfault, it might look weird, it should do nothing at all! But for the final time: Zero is necessary, and you have to put it in.
    Last edited by whiteflags; 06-12-2011 at 10:08 PM.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by guitargod
    Output- LASERLIGHT

    A null character isn't NECESSARY!
    When the siz eof the array is more than what we've filled, the compiler puts it in!!
    You're in the realm of undefined behaviour. Initialising arr with "LASERLIGHT" is fine (except that you got that wrong: the string literal should not be in braces), since I recall that as a special exception the null character in the string literal will not be written to arr[10]. However, printing arr with printf("%s", arr) results in undefined behaviour since the contents of arr is not a null terminated string (note that taking the address of arr is wrong). It so happens that it works for you, but for me I get "LASERLIGHT@" (note the '@').
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    30

    Cool Proof

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int main()
    {
        char arr[10]={"LASERLIGHT"};
        printf("%s\n", &arr);
        strcat(arr, "ING");
        printf("%s\n", &arr);
        strcpy(arr, "WHITEFLAG");
        printf("%s\n", &arr);
        getch();
        return 0;
    }
    Output-
    LASERLIGHT
    LASERLIGHTING
    WHITEFLAG
    ...

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    guitargod: you're not paying attention. It doesn't matter what program you show us: you're wrong. Any program output that you provide is just by luck. If you want to try finding a case where a problem is demonstrated, try creating arrays before and after arr, then print their contents. Aligning the size of the arrays to word boundaries may also help.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-03-2011, 12:29 AM
  2. i dont understand bit
    By joker_tony in forum C Programming
    Replies: 2
    Last Post: 03-27-2008, 12:15 AM
  3. need help to understand a piece of code
    By rraajjiibb in forum C Programming
    Replies: 3
    Last Post: 08-09-2004, 11:18 PM
  4. What I dont understand...
    By nomi in forum C# Programming
    Replies: 7
    Last Post: 01-20-2004, 02:00 AM
  5. dont understand VS.NET
    By Qui in forum Windows Programming
    Replies: 6
    Last Post: 10-15-2003, 07:36 PM