Results 1 to 3 of 3

Thread: Copying a pointer to a static 2D array

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Copying a pointer to a static 2D array

    Hello!

    This is actually a question about C and not so much about Qt. I declare a static 2D array of ints like this:

    int i[2][2];
    i[0][0] = 1;
    i[0][1] = 2;
    i[1][0] = 3;
    i[1][1] = 4;

    And now I would like to have a second pointer j, that points to the same array.

    int** j = i;

    But the compiler doesn't allow me to do that: "cannot convert ‘int (*)[2]’ to ‘int**’ in initialization".
    Trying to "hack" it with a memcpy

    memcpy((void*)&j, (void*)i, sizeof(long int));

    leads to a segmentation fault.

    What am I not doing right?

    Thanks,
    Cruz

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Copying a pointer to a static 2D array

    int *j = i;

    j[0][0] = 1;
    j[1][1] = 2;

    or just j[3] = 4;

    etc.

  3. #3
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Copying a pointer to a static 2D array

    Or int (*j)[2] = i;
    and then j[0][0] = 100;

Similar Threads

  1. structs, pointer, static callback function
    By vonCZ in forum General Programming
    Replies: 3
    Last Post: 20th June 2008, 12:53
  2. static Object Vs Pointer
    By rajeshs in forum General Programming
    Replies: 4
    Last Post: 11th June 2008, 07:41
  3. Replies: 2
    Last Post: 16th April 2008, 10:31
  4. Copying row from one table to another
    By ser_bur in forum Qt Programming
    Replies: 2
    Last Post: 29th May 2007, 15:25
  5. Pointer to a 2D array of pointers ??
    By aamer4yu in forum General Programming
    Replies: 2
    Last Post: 1st February 2007, 11:16

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.