Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
02/25/08 15:50
Modified:
  02/25/08 15:56

Read: times


 
#151422 - I think not
Responding to: ???'s previous message
Charles,

Thanks for taking the time to code the example. Unfortunately, it's wrong.

The biggest problem is with this line:
   ++phrases2[line_no];
That statement tries to increment one of the entries in the phrases2[] table. If the table is in code memory as we have been discussing, that simply won't work. And even if the table was in RAM where you could modify it, doing so would corrupt the table so that the program would fail the second time through the main loop.

What you need instead is to pass a pointer to each phrase as the argument to put_phrase(). That way, put_phrase() gets its own private pointer to each phrase, which it can manipulate as needed without corrupting the table.

The following code shows what I mean. (It's modified for simulation on the PC so that I could actually run it to make sure it works.) Note that it also shows Michael's nifty technique for automatically #defining MAX_LINES so you don't have to worry about changing it every time you change the phrases[] table.

-- Russ
#include <stdio.h>

unsigned char *phrases[] = {                  // Array of pointers to strings
    "Short",                                  //  to display
    "Medium",
    "Very long"
    };                                        // End phrases[] table

#define MAX_LINES (sizeof(phrases) / sizeof(char *))

/* ///////////////////////////////////////////////////////////////////////// */
    
void put_phrase (                             // Displays the string pointed
    unsigned char *phrasePointer              //  to by 'phrasePointer'
    ) {

    while (*phrasePointer != 0) {             // For each character in string
        putchar(*phrasePointer++);            // Display the character
        }                                     // End 'for each character'
    putchar('\n');                            // Add newline for testing on PC
    }                                         // End put_phrase()

/* ///////////////////////////////////////////////////////////////////////// */
    
void main( void ) {                           // Exercises put_phrase()

    int i;                                    // Generic loop counter
    unsigned char phrase;                     // Index into phrases[] table

    for (i=0; i<5; i++) {                     // Do 5 times for testing on PC
        for (phrase = 0;                      // For each phrase in the table
             phrase < MAX_LINES;
             phrase++) {
            put_phrase(phrases[phrase]);      // Display the current phrase
            }                                 // End 'for each phrase'
        }                                     // End 'do 5 times'
    }                                         // End main()



List of 54 messages in thread
TopicAuthorDate
Best way to consolidate....out of memory            01/01/70 00:00      
   isn't this the classical gotcha?            01/01/70 00:00      
   There was a hint here....            01/01/70 00:00      
      Not const            01/01/70 00:00      
         Not necessarily            01/01/70 00:00      
            Hmmm...            01/01/70 00:00      
               Hmmm, indeed            01/01/70 00:00      
               Why?            01/01/70 00:00      
                  One reason ... and the prolly the original intent            01/01/70 00:00      
                     Obviously            01/01/70 00:00      
                        Speed            01/01/70 00:00      
                           Rogue programs?            01/01/70 00:00      
                              Yes. And stupid programmers            01/01/70 00:00      
                                 True            01/01/70 00:00      
                           cases            01/01/70 00:00      
                        The Obvious...            01/01/70 00:00      
                           Fair enough            01/01/70 00:00      
                              make sure the developer doesn't do something stupi            01/01/70 00:00      
                              Const to Code EPROM or FLASH,,,            01/01/70 00:00      
                           const and volatile - for optimization            01/01/70 00:00      
                  consts in other than CODE space            01/01/70 00:00      
                     Use the extended keywords            01/01/70 00:00      
                        'const' and 'volatile'            01/01/70 00:00      
   Unreadable code!            01/01/70 00:00      
      It really was all Keils fault....;)            01/01/70 00:00      
   You don't want to start from here!            01/01/70 00:00      
      Previously, on 8052.com...            01/01/70 00:00      
      Array of string            01/01/70 00:00      
         Yes, my mistake            01/01/70 00:00      
   string concatenate            01/01/70 00:00      
      Comments            01/01/70 00:00      
         Wow......LOL            01/01/70 00:00      
            Not a C vs ASM thing            01/01/70 00:00      
               From your perspective, it may make sense            01/01/70 00:00      
      I went this direction....code padding removal?            01/01/70 00:00      
         For but one byte added to the array...            01/01/70 00:00      
         With proper ordering in source file...            01/01/70 00:00      
         Putting it all together            01/01/70 00:00      
            Spectacular Russ.....            01/01/70 00:00      
   THINK            01/01/70 00:00      
      that is surely needed            01/01/70 00:00      
      A bit easier to read?            01/01/70 00:00      
         easy to read???            01/01/70 00:00      
      typedef vs #define            01/01/70 00:00      
      I think not            01/01/70 00:00      
         Tks Russ.            01/01/70 00:00      
            THINK(ing)            01/01/70 00:00      
               ???            01/01/70 00:00      
                  Objectives.            01/01/70 00:00      
                     I beg your pardon?!            01/01/70 00:00      
                     I see            01/01/70 00:00      
   Best way to consolidate....out of memory            01/01/70 00:00      
   Why specify CODE for functions?            01/01/70 00:00      
      there is so much stuff            01/01/70 00:00      

Back to Subject List