Results 1 to 4 of 4

Thread: 2D pointer assignment

  1. #1
    Join Date
    Aug 2013
    Posts
    41
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    4

    Default 2D pointer assignment

    Hello,

    I have two 2D pointers.
    float **A;
    float **B;
    both have two rows and each row has 10 columns.
    I would only like to copy the elements of B[1] to A[0] that is from second row of B to first row of A.

    So, I wrote:

    Qt Code:
    1. A[0] = B[1];
    To copy to clipboard, switch view to plain text mode 

    This does not work! The above assignment copies all the elements from both the rows of B to A.
    What am I missing??

  2. #2
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 11 Times in 11 Posts

    Default Re: 2D pointer assignment

    Hi,
    You need to give size of copying data.
    Example A and B as 2D integer array have size n rows and m columns.
    So, if you wan to copy element from B[1] to A[0], you can use command :
    Qt Code:
    1. memcpy(A[0], B[1], m*sizeof(int))
    To copy to clipboard, switch view to plain text mode 

    The other method is you can loop the value from your array.
    But, I think the first method is more simple.

    best regards,

    Toto

  3. #3
    Join Date
    Aug 2013
    Posts
    41
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    4

    Default Re: 2D pointer assignment

    @myta212: Thanks for ur response.

    But the problem is that it should be done during run time. The indices(rows) which should be shifted are updated according to the selection in QComboBox.
    And the processing is done frame-wise in real time. By using memcpy I only copy one frame. But I want to shift the entire row (which is computed during run time frame by frame).

    Therefore, I would only like to switch the row numbers. There is any solution which suits my requirement?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: 2D pointer assignment

    Your original attempt did not copy anything at all, it just changed a pointer (and caused a memory leak in all likelihood).
    Qt Code:
    1. A[0] = B[1];
    To copy to clipboard, switch view to plain text mode 
    This does not work! The above assignment copies all the elements from both the rows of B to A.
    If it does what you describe then you don't have the pointer to a list of pointers to int you think you have.

    But the problem is that it should be done during run time.
    Why do you think using memcpy() cannot meet this requirement? The example shown above copies an entire row.

    You have a C++ compiler, why are you using C-style structures with all of their memory allocation and sizing pitfalls? The way to do this is with a QVector<QVector<int> > or a similar structure using the C++ standard library or Boost.
    Qt Code:
    1. QVector<QVector<int> > A;
    2. QVector<QVector<int> > B;
    3. A.resize(rows);
    4. B.resize(rows);
    5. for (int r = 0; r < rows; ++r) {
    6. A[r].resize(cols);
    7. B[r].resize(cols);
    8. for (int c = 0; c < cols; ++c) {
    9. A[r][c] = 0;
    10. B[r][c] = c;
    11. }
    12. }
    13. qDebug() << "A Before" << A;
    14. qDebug() << "B Before" << B;
    15. AA[0] = BB[1];
    16. qDebug() << "A After " << A;
    17. qDebug() << "B After " << B;
    18. // A Before QVector(QVector(0, 0, 0) , QVector(0, 0, 0) )
    19. // B Before QVector(QVector(0, 1, 2) , QVector(0, 1, 2) )
    20. // A After QVector(QVector(0, 1, 2) , QVector(0, 0, 0) )
    21. // B After QVector(QVector(0, 1, 2) , QVector(0, 1, 2) )
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. c++ code assignment
    By alexrat1996 in forum Qt Programming
    Replies: 5
    Last Post: 15th February 2013, 20:01
  2. Replies: 1
    Last Post: 4th December 2010, 18:20
  3. QSettings Assignment
    By SixDegrees in forum Qt Programming
    Replies: 1
    Last Post: 1st May 2010, 00:00
  4. QString assignment
    By valgaba in forum Qt Programming
    Replies: 4
    Last Post: 25th April 2010, 18:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.