Const string arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Toe001
    New Member
    • Aug 2007
    • 8

    Const string arrays

    Can you please help me understand why I do not see the answer expected (Parity: Odd). Can I have an array of strings as constants? How do I index each string in the array?

    const Parity[3] = {'None', 'Odd', 'Even'};
    char string[];

    sprintf(string, "Parity: %s", Parity[1]);
    printf(string);

    Thanks
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    You got the right idea but your code has syntax problems. Does this compile for you? I doubt it.

    Your Parity definition says its a "const Parity" but it does not tell the compiler what it is ( i.e. int, char, pointer to something?) Put that something between the const and Parity. Also, use the double quote instead of the single quote for strings. The single quote defines a single char, not a string.

    Finally, your string variable needs some size. Pick a value bigger than the largest string in Parity.

    Comment

    • Toe001
      New Member
      • Aug 2007
      • 8

      #3
      Thank you for the help.

      Yes it did compile without errors (it is a Z8 micro compiler), so this is why I was confused!

      I have followed your suggestions and changed to:

      const char Parity[3][4] = {"None", "Odd", "Even"};
      char string[];

      sprintf(string, "Parity: %s", Parity[1]);
      printf(string);

      And it now seems to work. However, this does not seem very elegant to have to use a multi-dimension array to hold these three constants (strings), is there a better way? Maybe I should I be using ennumerated types rather?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        If you don't need the items as strings, then an enum is the way to go.

        All an enum is a list of named integer values. You use enums to avoid hard-coded values in your program:
        [code=cpp]
        enum Value {NONE, ODD, EVEN};

        if (something == ODD)
        {
        //etc...
        }
        [/code]

        Since an enum is a named integer value and not an actual integer, an enum is a declaration and that means you can put it in a header file and use it in all of your source files.

        Comment

        • Toe001
          New Member
          • Aug 2007
          • 8

          #5
          Thank you for this further explanation of enum types. It seems a better way to go about things and I will try this.

          For my education - and I am a beginner - can one not have in C a simple array with three elements to hold three strings, and so as not to allow these strings to be altered, add the qualifier "const". Something like:

          const char Parity[3] = {"None", "Odd", "Even"};

          It seems straight forward, but does not work?

          I would expect Parity[1] to hold the string "Odd" etc.

          Thanks

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by Toe001
            Thank you for this further explanation of enum types. It seems a better way to go about things and I will try this.

            For my education - and I am a beginner - can one not have in C a simple array with three elements to hold three strings, and so as not to allow these strings to be altered, add the qualifier "const". Something like:

            const char Parity[3] = {"None", "Odd", "Even"};

            It seems straight forward, but does not work?

            I would expect Parity[1] to hold the string "Odd" etc.

            Thanks
            Each element is a char so 1 letter. You need an array of char *s:
            [code=cpp]
            char *Parity[3] = {"None", "Odd", "Even"};
            [/code]

            Comment

            • Toe001
              New Member
              • Aug 2007
              • 8

              #7
              Aah, now I understand!

              Thank you all.

              Comment

              Working...