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. Two dimmensional array using pointers on Arduino Due

Two dimmensional array using pointers on Arduino Due

Scheduled Pinned Locked Moved C / C++ / MFC
debugginghardwarequestiondata-structures
9 Posts 5 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.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    I am trying to build a two dimmensional array - accessing LCD of 10 rows and 20 columns and be able to pass the pointer(s) to a function.
    The function / method is declared in class and so is the pointer:

    boolean DisplayImage(int \*\*Display);
    int \*\*Display;
    

    And the function:

    boolean CImage::DisplayImage(int **Display)
    {

    #ifdef DEBUG
    TRACE("CImage::DisplayImage ** ", 1);
    #endif

    int i, j, iRow, iCol;
    iRow = 10;
    iCol = 20;
    int iCount = 0;

    for ( i = 0; i < iRow; i++) {
    for (j = 0; j < iCol; j++) {
    lcd_i2c.clear();
    lcd_i2c.print("location ");
    lcd_i2c.setCursor(0, 1);
    lcd_i2c.print("row i ");
    lcd_i2c.print(i );
    lcd_i2c.print(" col j ");
    lcd_i2c.print(j );
    lcd_i2c.setCursor(0, 2);
    lcd_i2c.print("Display ");
    **Display = iCount++; // set test value - works
    lcd_i2c.print(**Display); // display is OK
    // lcd_i2c.setCursor(0, 3);
    // lcd_i2c.print("mask center ");
    // lcd_i2c.print(**image );
    **Display++;
    delay(500);
    }
    }

    return true;
    }

    Here is how I initialize the array

    CImage::CImage (void)
    {
    // initialize array using pointers
    // int **Display;
    Display = new int *[10]; // display rows
    for (int i = 0; i < 20; i++)
    Display[i] = new int[10]; // display columns

    }

    Here are my questions: 1. I need some explanation about this syntax "Display = new int *[10]" - is the "Display " the variable name ? 2. I may have the rows / columns reversed because I really do not understand how is the array initialized.( That is my main question - I can take it from there ). 3. Unfortunatelly the example I got this from had 10 by 10 array so I am lost as far as what are the columns and what are the rows. 4. I use rows and columns because that is the underlying hardware and I understand it is irrelevant as far as multidimmensioal array goes. 5. Under present initialization I cannot get the LCD print pass first "row". The processor just stops ( Arduino Due). I would expect it to keep printing even if I runs out of valid pointer, but that is not the case. I sure would like to know why. ( It "runs" GCC compiler). Thank you for your time and help. Appreciate it. Cheers Vaclav PS I do not have a real debugger and using the LCD as "trace / debug "

    A D L 3 Replies Last reply
    0
    • V Vaclav_

      I am trying to build a two dimmensional array - accessing LCD of 10 rows and 20 columns and be able to pass the pointer(s) to a function.
      The function / method is declared in class and so is the pointer:

      boolean DisplayImage(int \*\*Display);
      int \*\*Display;
      

      And the function:

      boolean CImage::DisplayImage(int **Display)
      {

      #ifdef DEBUG
      TRACE("CImage::DisplayImage ** ", 1);
      #endif

      int i, j, iRow, iCol;
      iRow = 10;
      iCol = 20;
      int iCount = 0;

      for ( i = 0; i < iRow; i++) {
      for (j = 0; j < iCol; j++) {
      lcd_i2c.clear();
      lcd_i2c.print("location ");
      lcd_i2c.setCursor(0, 1);
      lcd_i2c.print("row i ");
      lcd_i2c.print(i );
      lcd_i2c.print(" col j ");
      lcd_i2c.print(j );
      lcd_i2c.setCursor(0, 2);
      lcd_i2c.print("Display ");
      **Display = iCount++; // set test value - works
      lcd_i2c.print(**Display); // display is OK
      // lcd_i2c.setCursor(0, 3);
      // lcd_i2c.print("mask center ");
      // lcd_i2c.print(**image );
      **Display++;
      delay(500);
      }
      }

      return true;
      }

      Here is how I initialize the array

      CImage::CImage (void)
      {
      // initialize array using pointers
      // int **Display;
      Display = new int *[10]; // display rows
      for (int i = 0; i < 20; i++)
      Display[i] = new int[10]; // display columns

      }

      Here are my questions: 1. I need some explanation about this syntax "Display = new int *[10]" - is the "Display " the variable name ? 2. I may have the rows / columns reversed because I really do not understand how is the array initialized.( That is my main question - I can take it from there ). 3. Unfortunatelly the example I got this from had 10 by 10 array so I am lost as far as what are the columns and what are the rows. 4. I use rows and columns because that is the underlying hardware and I understand it is irrelevant as far as multidimmensioal array goes. 5. Under present initialization I cannot get the LCD print pass first "row". The processor just stops ( Arduino Due). I would expect it to keep printing even if I runs out of valid pointer, but that is not the case. I sure would like to know why. ( It "runs" GCC compiler). Thank you for your time and help. Appreciate it. Cheers Vaclav PS I do not have a real debugger and using the LCD as "trace / debug "

      A Offline
      A Offline
      Albert Holguin
      wrote on last edited by
      #2

      Vaclav_Sal wrote:

      1. I need some explanation about this syntax "Display = new int *[10]" - is the "Display " the variable name ?

      Yes

      Vaclav_Sal wrote:

      2. I may have the rows / columns reversed because<b> I really do not understand how is the array initialized</b>.( <b><b></b>That is my main question</b> - I can take it from there ).

      This is actually a matter of how the array is used (i.e. however you define it, or if you're using someone else's code, how they define it) since it's not really a matrix.

      Vaclav_Sal wrote:

      3. Unfortunatelly the example I got this from had 10 by 10 array so I am lost as far as what are the columns and what are the rows.

      Without looking at the code, your guess is as good as mine... :)

      Vaclav_Sal wrote:

      5. Under present initialization I cannot get the LCD print pass first "row". The processor just stops ( Arduino Due). I would expect it to keep printing even if I runs out of valid pointer, but that is not the case. I sure would like to know why. ( It "runs" GCC compiler).

      Does it say anything when it stops? ...is it crashing?

      V 4 2 Replies Last reply
      0
      • A Albert Holguin

        Vaclav_Sal wrote:

        1. I need some explanation about this syntax "Display = new int *[10]" - is the "Display " the variable name ?

        Yes

        Vaclav_Sal wrote:

        2. I may have the rows / columns reversed because<b> I really do not understand how is the array initialized</b>.( <b><b></b>That is my main question</b> - I can take it from there ).

        This is actually a matter of how the array is used (i.e. however you define it, or if you're using someone else's code, how they define it) since it's not really a matrix.

        Vaclav_Sal wrote:

        3. Unfortunatelly the example I got this from had 10 by 10 array so I am lost as far as what are the columns and what are the rows.

        Without looking at the code, your guess is as good as mine... :)

        Vaclav_Sal wrote:

        5. Under present initialization I cannot get the LCD print pass first "row". The processor just stops ( Arduino Due). I would expect it to keep printing even if I runs out of valid pointer, but that is not the case. I sure would like to know why. ( It "runs" GCC compiler).

        Does it say anything when it stops? ...is it crashing?

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

        It basically stops here lcd_i2c.print(**Display); // display is OK So it may be the LCD problem. If it crashed it should restart, but there is something different is Due (SAM) code from AVR code on start up, so it my be just stuck. I think I am assigning the values to the wrong pointer after the first array is done. I actually should use plain array and not pointers since I need to do convolution with 3x3 matrix to detect edge. I am not sure this is correct - the function gets a double pointer and than I assign to double pointer. But if I assign Display = iCount the compiler does not like it and I am still learning how to "translate" compiler / Arduino error messages.

            lcd\_i2c.setCursor(0, 2);
             lcd\_i2c.print("Display   ");
             \*\*Display = iCount++;         // set test value - works 
             lcd\_i2c.print(\*\*Display);   // display is OK 
             //         lcd\_i2c.setCursor(0, 3);
             //         lcd\_i2c.print("mask center ");
             //         lcd\_i2c.print(\*\*image );
             \*\*Display++;
             delay(500);
        
        1 Reply Last reply
        0
        • V Vaclav_

          I am trying to build a two dimmensional array - accessing LCD of 10 rows and 20 columns and be able to pass the pointer(s) to a function.
          The function / method is declared in class and so is the pointer:

          boolean DisplayImage(int \*\*Display);
          int \*\*Display;
          

          And the function:

          boolean CImage::DisplayImage(int **Display)
          {

          #ifdef DEBUG
          TRACE("CImage::DisplayImage ** ", 1);
          #endif

          int i, j, iRow, iCol;
          iRow = 10;
          iCol = 20;
          int iCount = 0;

          for ( i = 0; i < iRow; i++) {
          for (j = 0; j < iCol; j++) {
          lcd_i2c.clear();
          lcd_i2c.print("location ");
          lcd_i2c.setCursor(0, 1);
          lcd_i2c.print("row i ");
          lcd_i2c.print(i );
          lcd_i2c.print(" col j ");
          lcd_i2c.print(j );
          lcd_i2c.setCursor(0, 2);
          lcd_i2c.print("Display ");
          **Display = iCount++; // set test value - works
          lcd_i2c.print(**Display); // display is OK
          // lcd_i2c.setCursor(0, 3);
          // lcd_i2c.print("mask center ");
          // lcd_i2c.print(**image );
          **Display++;
          delay(500);
          }
          }

          return true;
          }

          Here is how I initialize the array

          CImage::CImage (void)
          {
          // initialize array using pointers
          // int **Display;
          Display = new int *[10]; // display rows
          for (int i = 0; i < 20; i++)
          Display[i] = new int[10]; // display columns

          }

          Here are my questions: 1. I need some explanation about this syntax "Display = new int *[10]" - is the "Display " the variable name ? 2. I may have the rows / columns reversed because I really do not understand how is the array initialized.( That is my main question - I can take it from there ). 3. Unfortunatelly the example I got this from had 10 by 10 array so I am lost as far as what are the columns and what are the rows. 4. I use rows and columns because that is the underlying hardware and I understand it is irrelevant as far as multidimmensioal array goes. 5. Under present initialization I cannot get the LCD print pass first "row". The processor just stops ( Arduino Due). I would expect it to keep printing even if I runs out of valid pointer, but that is not the case. I sure would like to know why. ( It "runs" GCC compiler). Thank you for your time and help. Appreciate it. Cheers Vaclav PS I do not have a real debugger and using the LCD as "trace / debug "

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

          Vaclav_Sal wrote:

          Display = new int *[10];            // display rows
             for (int i = 0; i < 20; i++)

          I question this. Shouldn't the for() loop only run 10 times instead of 20?

          "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 trying to build a two dimmensional array - accessing LCD of 10 rows and 20 columns and be able to pass the pointer(s) to a function.
            The function / method is declared in class and so is the pointer:

            boolean DisplayImage(int \*\*Display);
            int \*\*Display;
            

            And the function:

            boolean CImage::DisplayImage(int **Display)
            {

            #ifdef DEBUG
            TRACE("CImage::DisplayImage ** ", 1);
            #endif

            int i, j, iRow, iCol;
            iRow = 10;
            iCol = 20;
            int iCount = 0;

            for ( i = 0; i < iRow; i++) {
            for (j = 0; j < iCol; j++) {
            lcd_i2c.clear();
            lcd_i2c.print("location ");
            lcd_i2c.setCursor(0, 1);
            lcd_i2c.print("row i ");
            lcd_i2c.print(i );
            lcd_i2c.print(" col j ");
            lcd_i2c.print(j );
            lcd_i2c.setCursor(0, 2);
            lcd_i2c.print("Display ");
            **Display = iCount++; // set test value - works
            lcd_i2c.print(**Display); // display is OK
            // lcd_i2c.setCursor(0, 3);
            // lcd_i2c.print("mask center ");
            // lcd_i2c.print(**image );
            **Display++;
            delay(500);
            }
            }

            return true;
            }

            Here is how I initialize the array

            CImage::CImage (void)
            {
            // initialize array using pointers
            // int **Display;
            Display = new int *[10]; // display rows
            for (int i = 0; i < 20; i++)
            Display[i] = new int[10]; // display columns

            }

            Here are my questions: 1. I need some explanation about this syntax "Display = new int *[10]" - is the "Display " the variable name ? 2. I may have the rows / columns reversed because I really do not understand how is the array initialized.( That is my main question - I can take it from there ). 3. Unfortunatelly the example I got this from had 10 by 10 array so I am lost as far as what are the columns and what are the rows. 4. I use rows and columns because that is the underlying hardware and I understand it is irrelevant as far as multidimmensioal array goes. 5. Under present initialization I cannot get the LCD print pass first "row". The processor just stops ( Arduino Due). I would expect it to keep printing even if I runs out of valid pointer, but that is not the case. I sure would like to know why. ( It "runs" GCC compiler). Thank you for your time and help. Appreciate it. Cheers Vaclav PS I do not have a real debugger and using the LCD as "trace / debug "

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

            You have two main problems. Firstly you create 10 rows and then try to initialise 20 of them each with 10 columns, try:

            int **Display = new int *[10]; // display rows
            for (i = 0; i < 10; i++) // initialise each of 10 rows
            Display[i] = new int[20]; // with 20 columns

            When adding your counter values into the array you increment your Display pointer (which indexes the rows only), which means that after 10 iterations it is pointing to an invalid address. Try using the offset values for row and column instead, as it will also make your code clearer, thus:

            // iterate through the 10 rows
            for ( i = 0; i < iRow; i++)
            {
            // for each column in the current row
            for (j = 0; j < iCol; j++)
            {
            // set the value of count
            Display[i][j] = iCount;
            ++iCount;
            }
            }

            V 1 Reply Last reply
            0
            • L Lost User

              You have two main problems. Firstly you create 10 rows and then try to initialise 20 of them each with 10 columns, try:

              int **Display = new int *[10]; // display rows
              for (i = 0; i < 10; i++) // initialise each of 10 rows
              Display[i] = new int[20]; // with 20 columns

              When adding your counter values into the array you increment your Display pointer (which indexes the rows only), which means that after 10 iterations it is pointing to an invalid address. Try using the offset values for row and column instead, as it will also make your code clearer, thus:

              // iterate through the 10 rows
              for ( i = 0; i < iRow; i++)
              {
              // for each column in the current row
              for (j = 0; j < iCol; j++)
              {
              // set the value of count
              Display[i][j] = iCount;
              ++iCount;
              }
              }

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

              Thanks, I was pretty sure I was doing it wrong using pointers while assigning the values. I also had the pointers array redefined in constructor. Sure would be easier with real debugger, oh well. Now it plays nicely. Thanks Cheers Vaclav

              L 1 Reply Last reply
              0
              • V Vaclav_

                Thanks, I was pretty sure I was doing it wrong using pointers while assigning the values. I also had the pointers array redefined in constructor. Sure would be easier with real debugger, oh well. Now it plays nicely. Thanks Cheers Vaclav

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

                Happy to help.

                Vaclav_Sal wrote:

                I was pretty sure I was doing it wrong using pointers

                I had to think for quite a while about that before I realised why it was not working.

                1 Reply Last reply
                0
                • A Albert Holguin

                  Vaclav_Sal wrote:

                  1. I need some explanation about this syntax "Display = new int *[10]" - is the "Display " the variable name ?

                  Yes

                  Vaclav_Sal wrote:

                  2. I may have the rows / columns reversed because<b> I really do not understand how is the array initialized</b>.( <b><b></b>That is my main question</b> - I can take it from there ).

                  This is actually a matter of how the array is used (i.e. however you define it, or if you're using someone else's code, how they define it) since it's not really a matrix.

                  Vaclav_Sal wrote:

                  3. Unfortunatelly the example I got this from had 10 by 10 array so I am lost as far as what are the columns and what are the rows.

                  Without looking at the code, your guess is as good as mine... :)

                  Vaclav_Sal wrote:

                  5. Under present initialization I cannot get the LCD print pass first "row". The processor just stops ( Arduino Due). I would expect it to keep printing even if I runs out of valid pointer, but that is not the case. I sure would like to know why. ( It "runs" GCC compiler).

                  Does it say anything when it stops? ...is it crashing?

                  4 Offline
                  4 Offline
                  4india
                  wrote on last edited by
                  #8

                  To make practice of pointer and Array related Question, As per my experience, I found one site. Which i recommend you you to make your concept clear. Here I found many problem category wise. Refer the page http://www.abhilashatechnology.com/p/cpp.html[^]

                  A 1 Reply Last reply
                  0
                  • 4 4india

                    To make practice of pointer and Array related Question, As per my experience, I found one site. Which i recommend you you to make your concept clear. Here I found many problem category wise. Refer the page http://www.abhilashatechnology.com/p/cpp.html[^]

                    A Offline
                    A Offline
                    Albert Holguin
                    wrote on last edited by
                    #9

                    You responded to me, not OP.

                    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