Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Accessing / printing two dimensional array of char using pointers

Accessing / printing two dimensional array of char using pointers

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++databasehardware
14 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • enhzflepE enhzflep

    Well, you can easily look at the data-sheet for the I2C chip on the LCD board and at the one for the 1602/1604/2002/2004 LCD you're using. Once done, you can see that the LCD has a hardware cursor, and knows at which position on the display it is. It also has a means to write a character to the current cursor position. Here's a quick list of the available commands: http://www.8051projects.net/lcd-interfacing/commands.php[^] Not covered in that particular document, is the fact that you define custom bitmaps for characters - up to 4 or so at any one time. You could for instance, make a bitmap of a battery and then animate a charging display by changing the bitmap used for the battery 'character'.(but not actually changing the character drawn - it's the bitmap that this character refers to that is changed) Here's a tool I whipped-up a couple of years back that makes drawing such custom characters easy. I've used this for both ascii/graphical LCDs - I don't remember if the output was suitable for both or just one or the other. Lcd Character designer The 'save' button causes the changes made to a character in the top-pane to be written to the 'memory' of the LCD panel and causes the appearance of the currently chosen character to change. As for the pointer stuff, you seem to be making it considerably more complex than it needs to be. Here's a quick sample. I dont have access to an arduino this moment, so have used printf instead.

    int main()
    {
    char *stringTable[] = { "string 1", "string two", "string three", NULL };

    int i, n = sizeof(stringTable) / sizeof(\*stringTable);
    
    // n-1 since the last element causes a crash.
    for (i=0; i<n-1; i++)
        printf("(%d chars): %s\\n", strlen(stringTable\[i\]), stringTable\[i\]);
    
    printf("-------------\\n");
    
    // a pointer to an array of pointers
    char \*\*curPtr = stringTable;
    
    do
    {
        printf("%s\\n", \*curPtr);    // dereference to get the ptr to the string
        curPtr++;                   // point to the next element in the array o string pointers
    } while ( \*curPtr );            // quit loop when we get to our NULL pointer.
    

    }

    "When I was 5 years old, my mother always told me that happine

    CPalliniC Offline
    CPalliniC Offline
    CPallini
    wrote on last edited by
    #5

    Why are you using both a sentinel value and the array size (just out of curiosity)?

    In testa che avete, signor di Ceprano?

    enhzflepE 1 Reply Last reply
    0
    • CPalliniC CPallini

      Why are you using both a sentinel value and the array size (just out of curiosity)?

      enhzflepE Offline
      enhzflepE Offline
      enhzflep
      wrote on last edited by
      #6

      Simply to show two ways of doing the same thing. Vaclav's code expects a sentinel, while I prefer to work without one. :)

      "When I was 5 years old, my mother always told me that happiness was the key to life. When I went to school, they asked me what I wanted to be when I grew up. I wrote down 'happy'. They told me I didn't understand the assignment, and I told them they didn't understand life." - John Lennon

      1 Reply Last reply
      0
      • V Vaclav_

        I am still struggling with pointers arithmetic. Here is my crude test code. I want to use pointers to access ( double - need triple eventually) the array of characters and do not get two things. Problem #1 is probably how does the LCD class do the "print" and I can figure that one by myself. I do not expect this forum to be familiar with Arduino "library". Problem # 2 I can print single characters in simple array by incrementing the pointer, but I cannot figure out why I cannot use same method on double array. I really do not understand what is this error telling me "error lvalue required as increment operand" - what is "lvalue" ? Please keep in mind that English is not my native language so go easy on " pointers to array..." "array of pointers" etc. As always , I appreciate your help. Cheers Vaclav Addendum OK, I did some more reading about how the array is initialized and how the NAME of the array cannot be used the way I did try it. So I did this - it "works" , but I am still not sure if it is correct or just a fluke. char *pointer = stringTable[0]; lcd_i2c.print((char*) pointer++); // prints entire line lcd_i2c.print((char*) pointer); // prints entire line starting with second character - expected that Original post code starts here char line[MAX_LINE] = "A TEST B"; int ncharacters = strlen(line); char *sptr; // pointer to memory block returned by malloc char *stringTable[NLINES]; // array of pointers to string int i = 0; sptr = (char*) malloc((unsigned) strlen(line) + 1); lcd_i2c.clear(); lcd_i2c.print("Index "); lcd_i2c.print(i); lcd_i2c.setCursor(0, 1); lcd_i2c.print("Length "); lcd_i2c.print(ncharacters ); lcd_i2c.setCursor(0, 2); lcd_i2c.print("pointer "); lcd_i2c.print(sptr); // problem #1 prints the entire line - not really a problem here strcpy(sptr, line); // copy line to pointed memory lcd_i2c.print(sptr); // TOK prints pointer value ? // copy pointer to first table array stringTable[0] = sptr; lcd_i2c.setCursor(0, 3); i = 0; do { // TOK prints first character from stringTable[0] lcd_i2c.print((char) * (*stringTable)); // problem #1 prints only the first character - why it would be nice to print the entire line using pointers! } while (*(*++stringTable)); error lvalue required as increment operand

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #7

        Vaclav_Sal wrote:

        Problem #1 is probably how does the LCD class do the "print" and I can figure that one by myself. I do not expect this forum to be familiar with Arduino "library".

        I use a 4x20 LCD display on my Arduino Uno and Mega boards. What are you trying to do?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        1 Reply Last reply
        0
        • V Vaclav_

          I am still struggling with pointers arithmetic. Here is my crude test code. I want to use pointers to access ( double - need triple eventually) the array of characters and do not get two things. Problem #1 is probably how does the LCD class do the "print" and I can figure that one by myself. I do not expect this forum to be familiar with Arduino "library". Problem # 2 I can print single characters in simple array by incrementing the pointer, but I cannot figure out why I cannot use same method on double array. I really do not understand what is this error telling me "error lvalue required as increment operand" - what is "lvalue" ? Please keep in mind that English is not my native language so go easy on " pointers to array..." "array of pointers" etc. As always , I appreciate your help. Cheers Vaclav Addendum OK, I did some more reading about how the array is initialized and how the NAME of the array cannot be used the way I did try it. So I did this - it "works" , but I am still not sure if it is correct or just a fluke. char *pointer = stringTable[0]; lcd_i2c.print((char*) pointer++); // prints entire line lcd_i2c.print((char*) pointer); // prints entire line starting with second character - expected that Original post code starts here char line[MAX_LINE] = "A TEST B"; int ncharacters = strlen(line); char *sptr; // pointer to memory block returned by malloc char *stringTable[NLINES]; // array of pointers to string int i = 0; sptr = (char*) malloc((unsigned) strlen(line) + 1); lcd_i2c.clear(); lcd_i2c.print("Index "); lcd_i2c.print(i); lcd_i2c.setCursor(0, 1); lcd_i2c.print("Length "); lcd_i2c.print(ncharacters ); lcd_i2c.setCursor(0, 2); lcd_i2c.print("pointer "); lcd_i2c.print(sptr); // problem #1 prints the entire line - not really a problem here strcpy(sptr, line); // copy line to pointed memory lcd_i2c.print(sptr); // TOK prints pointer value ? // copy pointer to first table array stringTable[0] = sptr; lcd_i2c.setCursor(0, 3); i = 0; do { // TOK prints first character from stringTable[0] lcd_i2c.print((char) * (*stringTable)); // problem #1 prints only the first character - why it would be nice to print the entire line using pointers! } while (*(*++stringTable)); error lvalue required as increment operand

          J Offline
          J Offline
          Jeremy Falcon
          wrote on last edited by
          #8

          Try asking one question at a time. If I have to spend 30 minutes trying to figure out what you're really getting at, I'm not too inclined to help. Help us help you. Keep the questions straightforward.

          Jeremy Falcon

          V 1 Reply Last reply
          0
          • J Jeremy Falcon

            Try asking one question at a time. If I have to spend 30 minutes trying to figure out what you're really getting at, I'm not too inclined to help. Help us help you. Keep the questions straightforward.

            Jeremy Falcon

            V Offline
            V Offline
            Vaclav_
            wrote on last edited by
            #9

            I am not sure what the readers problems are - my English or their inability to read. I do not like to engage in this kind of non technical discussions, have better things to do. But since you asked I said I can figure out the LCD "problem" - so I really don't understand why I am getting all the flack for mentioning it. I was trying to avoid " what are you trying to do?". So again , I have NO Problem with LCD , it is just a convenient way to track my code. And if it includes stuff you feel is weird so what. It has NOTHING to do with my MAIN question. And the question is in the title of this post. So far only ONE reply has given me REAL answer. I have been around here for a while and in past EVERYBODY was 100% courteous and very helpful. I am occasionally participating in Arduino forum where majority of "gurus" are nowhere near of caliber of knowledge as this forum is. They commonly switch subject, give lectures on OT , hamper on "post you full code", "what you doing?" , scrutinize coding style (if it does not lineup with their style) etc. Very egotistical and unfriendly bunch, especially to beginners. I am rather sadden to see that similar attitude is showing up on this forum. I am sorry I got on my soap box, but if you have no idea you are doing / posting in style not in line with this forum traditions and guidelines you should be told so. So that what I did. Please no more off the tech subject stuff. Cheers and have a nice one. Vaclav

            J L 2 Replies Last reply
            0
            • V Vaclav_

              I am not sure what the readers problems are - my English or their inability to read. I do not like to engage in this kind of non technical discussions, have better things to do. But since you asked I said I can figure out the LCD "problem" - so I really don't understand why I am getting all the flack for mentioning it. I was trying to avoid " what are you trying to do?". So again , I have NO Problem with LCD , it is just a convenient way to track my code. And if it includes stuff you feel is weird so what. It has NOTHING to do with my MAIN question. And the question is in the title of this post. So far only ONE reply has given me REAL answer. I have been around here for a while and in past EVERYBODY was 100% courteous and very helpful. I am occasionally participating in Arduino forum where majority of "gurus" are nowhere near of caliber of knowledge as this forum is. They commonly switch subject, give lectures on OT , hamper on "post you full code", "what you doing?" , scrutinize coding style (if it does not lineup with their style) etc. Very egotistical and unfriendly bunch, especially to beginners. I am rather sadden to see that similar attitude is showing up on this forum. I am sorry I got on my soap box, but if you have no idea you are doing / posting in style not in line with this forum traditions and guidelines you should be told so. So that what I did. Please no more off the tech subject stuff. Cheers and have a nice one. Vaclav

              J Offline
              J Offline
              Jeremy Falcon
              wrote on last edited by
              #10

              You have the wrong attitude.

              Jeremy Falcon

              1 Reply Last reply
              0
              • V Vaclav_

                I am not sure what the readers problems are - my English or their inability to read. I do not like to engage in this kind of non technical discussions, have better things to do. But since you asked I said I can figure out the LCD "problem" - so I really don't understand why I am getting all the flack for mentioning it. I was trying to avoid " what are you trying to do?". So again , I have NO Problem with LCD , it is just a convenient way to track my code. And if it includes stuff you feel is weird so what. It has NOTHING to do with my MAIN question. And the question is in the title of this post. So far only ONE reply has given me REAL answer. I have been around here for a while and in past EVERYBODY was 100% courteous and very helpful. I am occasionally participating in Arduino forum where majority of "gurus" are nowhere near of caliber of knowledge as this forum is. They commonly switch subject, give lectures on OT , hamper on "post you full code", "what you doing?" , scrutinize coding style (if it does not lineup with their style) etc. Very egotistical and unfriendly bunch, especially to beginners. I am rather sadden to see that similar attitude is showing up on this forum. I am sorry I got on my soap box, but if you have no idea you are doing / posting in style not in line with this forum traditions and guidelines you should be told so. So that what I did. Please no more off the tech subject stuff. Cheers and have a nice one. Vaclav

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #11

                Vaclav_Sal wrote:

                I am not sure what the readers problems are - my English or their inability to read.

                Neither, it's the fact that you question is rather over complicated, without actually explaining what the problem is. Hence my original reply to your question, which you ignored.

                1 Reply Last reply
                0
                • enhzflepE enhzflep

                  Well, you can easily look at the data-sheet for the I2C chip on the LCD board and at the one for the 1602/1604/2002/2004 LCD you're using. Once done, you can see that the LCD has a hardware cursor, and knows at which position on the display it is. It also has a means to write a character to the current cursor position. Here's a quick list of the available commands: http://www.8051projects.net/lcd-interfacing/commands.php[^] Not covered in that particular document, is the fact that you define custom bitmaps for characters - up to 4 or so at any one time. You could for instance, make a bitmap of a battery and then animate a charging display by changing the bitmap used for the battery 'character'.(but not actually changing the character drawn - it's the bitmap that this character refers to that is changed) Here's a tool I whipped-up a couple of years back that makes drawing such custom characters easy. I've used this for both ascii/graphical LCDs - I don't remember if the output was suitable for both or just one or the other. Lcd Character designer The 'save' button causes the changes made to a character in the top-pane to be written to the 'memory' of the LCD panel and causes the appearance of the currently chosen character to change. As for the pointer stuff, you seem to be making it considerably more complex than it needs to be. Here's a quick sample. I dont have access to an arduino this moment, so have used printf instead.

                  int main()
                  {
                  char *stringTable[] = { "string 1", "string two", "string three", NULL };

                  int i, n = sizeof(stringTable) / sizeof(\*stringTable);
                  
                  // n-1 since the last element causes a crash.
                  for (i=0; i<n-1; i++)
                      printf("(%d chars): %s\\n", strlen(stringTable\[i\]), stringTable\[i\]);
                  
                  printf("-------------\\n");
                  
                  // a pointer to an array of pointers
                  char \*\*curPtr = stringTable;
                  
                  do
                  {
                      printf("%s\\n", \*curPtr);    // dereference to get the ptr to the string
                      curPtr++;                   // point to the next element in the array o string pointers
                  } while ( \*curPtr );            // quit loop when we get to our NULL pointer.
                  

                  }

                  "When I was 5 years old, my mother always told me that happine

                  V Offline
                  V Offline
                  Vaclav_
                  wrote on last edited by
                  #12

                  Ok I think I am getting the picture. But I cannot figure out how to implement same pointer arithmetic in double array. I got the first "record" working fine, can access all strings / fields, but cannot get to the next one. I am still not sure how does the pointer , and not the char in array , get incremented. I have deleted the LCD code which confused many and added printf , which is not part of "core" Arduino code , so this should pass by them now. I still would like some verbal description of the assignment of pointer to the array. Appreciate your help very much. Cheers Vaclav

                  // double array of pointers
                  char *stringTable[][5] = {{ "string 1", "string two", "string three", NULL },
                  { "second string 1", "second string two", "second string three", NULL }

                  };

                  char **curPtr = *stringTable; // pointer to array of pointers

                  do
                  {
                  printf("%s\n", *curPtr++); // dereference to get the ptr to the string
                  } while ( *curPtr ); // quit loop when we get to our NULL pointer.

                  enhzflepE 1 Reply Last reply
                  0
                  • V Vaclav_

                    Ok I think I am getting the picture. But I cannot figure out how to implement same pointer arithmetic in double array. I got the first "record" working fine, can access all strings / fields, but cannot get to the next one. I am still not sure how does the pointer , and not the char in array , get incremented. I have deleted the LCD code which confused many and added printf , which is not part of "core" Arduino code , so this should pass by them now. I still would like some verbal description of the assignment of pointer to the array. Appreciate your help very much. Cheers Vaclav

                    // double array of pointers
                    char *stringTable[][5] = {{ "string 1", "string two", "string three", NULL },
                    { "second string 1", "second string two", "second string three", NULL }

                    };

                    char **curPtr = *stringTable; // pointer to array of pointers

                    do
                    {
                    printf("%s\n", *curPtr++); // dereference to get the ptr to the string
                    } while ( *curPtr ); // quit loop when we get to our NULL pointer.

                    enhzflepE Offline
                    enhzflepE Offline
                    enhzflep
                    wrote on last edited by
                    #13

                    No problem, it's one of the problems that seems to beat-up people regularly. I more or less started with ASM, so it was a matter of sink or swim in short order. How about this, does it help?

                    int main()
                    {
                    // double array of pointers
                    char *stringTable[][4] = {
                    { "string 1", "string two", "string three", NULL },
                    { "second string 1", "second string two", "second string three", NULL }
                    };

                    int y;
                    for (y=0; y<sizeof(stringTable)/sizeof(stringTable\[0\]); y++)   // y < (32/16) - 32bit code
                    {
                        char \*\*curPtr = stringTable\[y\];
                        do
                        {
                            printf("%s\\n", \*curPtr++);
                        } while (\*curPtr);
                        printf("----\\n");
                    }
                    

                    }

                    "When I was 5 years old, my mother always told me that happiness was the key to life. When I went to school, they asked me what I wanted to be when I grew up. I wrote down 'happy'. They told me I didn't understand the assignment, and I told them they didn't understand life." - John Lennon

                    V 1 Reply Last reply
                    0
                    • enhzflepE enhzflep

                      No problem, it's one of the problems that seems to beat-up people regularly. I more or less started with ASM, so it was a matter of sink or swim in short order. How about this, does it help?

                      int main()
                      {
                      // double array of pointers
                      char *stringTable[][4] = {
                      { "string 1", "string two", "string three", NULL },
                      { "second string 1", "second string two", "second string three", NULL }
                      };

                      int y;
                      for (y=0; y<sizeof(stringTable)/sizeof(stringTable\[0\]); y++)   // y < (32/16) - 32bit code
                      {
                          char \*\*curPtr = stringTable\[y\];
                          do
                          {
                              printf("%s\\n", \*curPtr++);
                          } while (\*curPtr);
                          printf("----\\n");
                      }
                      

                      }

                      "When I was 5 years old, my mother always told me that happiness was the key to life. When I went to school, they asked me what I wanted to be when I grew up. I wrote down 'happy'. They told me I didn't understand the assignment, and I told them they didn't understand life." - John Lennon

                      V Offline
                      V Offline
                      Vaclav_
                      wrote on last edited by
                      #14

                      Thanks, problem solved. Actually while "programming in my sleep" I thought that I must be missing advancing the "main" pointer by the whole record. But stringTable[y] is nice "trick". I really appreciate your help and as soon as I figure out how to add the characters into the record ( one at a time ) I'll be all set. You have a great day. Cheers Vaclav PS When I get ambitious I will try to figure out how to replace stringTable[y] using pointer.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups