Thread: I dont understand a piece of this code

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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
    ...

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    30
    Okzz... Give up!

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by guitargod
    Proof
    Proof of something alright... something about level of ignorance, or ability to troll!

    If you're not experienced enough to figure out what really happens when you don't leave room for the null terminator that it puts out of bounds from that array, then that's you're problem. Good luck tracking down the problem when the bug occurs in a real program. Maybe then you'll learn.

    You're wrong - end of story. If you don't stop and listen immediately then you are a troll and will be dealt with one way or another.
    Last edited by iMalc; 06-13-2011 at 12:46 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

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