How to stop processing static const unsigned char * const tab[][]

This is a discussion on How to stop processing static const unsigned char * const tab[][] within the C Programming forums, part of the General Programming Boards category; Hi, I have this code: Code: static const unsigned char *const Msgs[1][16] = { { "Please", "go to the", "user ...

  1. #1
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42

    Thumbs up How to stop processing static const unsigned char * const tab[][]

    Hi, I have this code:
    Code:
    static const unsigned char *const Msgs[1][16] =
    	{
    		{
    			"Please", "go to the", "user menu", "- tanks -",
    			"", 		"",		"",		"",
    			"",		"", 		"",		"",
    			"",		"",		"",		""
    		}
    	};
    .
    .
    .
    void fun(void)
    {
        unsigned char i;
    
        for (i = 0; Msgs[0][i]; i++)
            {
             printf("%s", (unsigned char *)Msgs[0][i]);
            }
    }
    I don't know do I aproach this problem correctly, but thing is I want to stop 'for loop' when there is nothing else to display. In fact Msgs[][] is much bigger and sometimes is has one string to display and sometimes 10, 15 etc. How can I detect end of data?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
        for (i = 0; Msgs[0][i][0]; i++)
            {
             printf("%s", (unsigned char *)Msgs[0][i]);
            }
    would break the loop when the string is "".

    Or, if you change your "" to NULL, you could use the code you originally wrote.

    Edit: Or just remove all of the "" bits, that will set the value to NULL by default [Pedantery: except for machines where NULL is not the same as a pointer with all bits set to zero - but I think those are extremely rare anyways]

    --
    Mats
    Last edited by matsp; 04-25-2008 at 08:10 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,659
    You can either store the number of elements or append a terminating null pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    And how can I get pointer to Msgs[][] and check that instead of Msgs[][][0] (btw that works fine). I'd like to use point as I have Msgs2 as well so just extra code to do something like Ptr=Msgs2 after I foud Msgs[][][0]==NULL or Ptr==NULL.

  5. #5
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by nenpa8lo View Post
    And how can I get pointer to Msgs[][] and check that instead of Msgs[][][0] (btw that works fine). I'd like to use point as I have Msgs2 as well so just extra code to do something like Ptr=Msgs2 after I foud Msgs[][][0]==NULL or Ptr==NULL.
    Do what matsp said, remove all the "" bits, and then assign the pointer:

    Code:
    unsigned char * cPtr;
    
    for(i = 0; cPtr = Msgs[0][i]; i++)
         printf("%s", cPtr);

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by IceDane View Post
    Do what matsp said, remove all the "" bits, and then assign the pointer:

    Code:
    unsigned char * cPtr;
    
    for(i = 0; cPtr = Msgs[0][i]; i++)
         printf("%s", cPtr);
    Why do you need a pointer? Yes, I agree that it avoids indexing the array twice, but the compiler probably does that anyways...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    Final looks like this:
    Code:
    static const unsigned char *const Msgs[1][16] =
    	{
    		{
    			"Please", "go to the", "user menu", "- tanks -",
    			"", 		"",		"",		"",
    			"",		"", 		"",		"",
    			"",		"",		"",		""
    		},
                    {...},  /* More msgs here */
                    {...},  /* And even more */
                    {...}
    	};
    .
    .
    .
    void DisplayMsg(unsigned int MsgNum)
    {
        unsigned char i;
    
        for (i = 0; *Msgs[MsgNum][i]; i++)
            {
             printf("%s", (unsigned char *)Msgs[MsgNum][i]);
            }
    }
    BTW NULL doesn't compile when repleacing "". I use IAR compiler for 16bit embedded microcontroller H8.

  8. #8
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by matsp View Post
    Why do you need a pointer? Yes, I agree that it avoids indexing the array twice, but the compiler probably does that anyways...

    --
    Mats
    Thought that was what he was asking. He says something about a pointer to Msgs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Need Help Please
    By YouShallNotPass in forum C++ Programming
    Replies: 3
    Last Post: 08-22-2006, 11:22 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21