Results 1 to 4 of 4

Thread: cofused on pointers

  1. #1
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default cofused on pointers

    guys,

    what does this definition mean?
    Qt Code:
    1. MyGrid ***grid;
    To copy to clipboard, switch view to plain text mode 

    please enlighten me

    baray98

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: cofused on pointers

    Quote Originally Posted by baray98 View Post
    guys,

    what does this definition mean?
    Qt Code:
    1. MyGrid ***grid;
    To copy to clipboard, switch view to plain text mode 

    please enlighten me

    baray98

    It means that grid - is a pointer which points at another pointer that points at one more pointer on MyGrid object.
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cofused on pointers

    I've never seen a triple pointer before, but there's no reason why not. A double pointer is somewhat common, it means a pointer to a pointer. Why would you want a pointer to a pointer? Consider c-style strings.

    char *mystring
    char **ptr2mystring;

    Also, arrays are really pointers to the first element of the array. So a common idiom is to write the following:

    int main(int argc, char* argv[])

    which is an array of strings as...

    int main(int argc, char** argv)

    But a triple pointer is much rarer. I would avoid it for the confusion it would cause. I would also avoid double pointers in C++ code, though they might be necessary in C code.

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: cofused on pointers

    It is not possible to say what the intention of a triple pointer is from the pointer itself - because C does not distinguish between pointers and arrays.

    It might be something not such sensible as the adress of the address of the adress of some object.
    Or it might be: a 3-dim array.
    int x; // an integer
    int *x; // address of an integer OR first element of an array (size unknown)
    int **x; // address of adress of integer; or address of array of int; or array of arrays of int
    int ***x; // ... or array of array of array of int

    (Note that the following is not correct
    int field[10][5][3];
    int ***x = field;

    an array of pointers is how one realizes in low-level, plain C a dynamic array of unknown size. Note that the rows may have different sizes: each is the result of a malloc with potentially different length.
    These things are not difficult but error prone. Therefore I recommend to stay clear of this and use C++/Qt containers when possible.)

Similar Threads

  1. Sorting using qSort(), - if QList contains POINTERS
    By joseph in forum Qt Programming
    Replies: 13
    Last Post: 18th August 2013, 18:55
  2. pointers and leaks
    By janus in forum General Programming
    Replies: 3
    Last Post: 11th October 2008, 21:26
  3. Problems passing an array of pointers to objects to a function
    By Valheru in forum General Programming
    Replies: 16
    Last Post: 30th June 2008, 00:11
  4. array of pointers to QtcpSocket
    By gQt in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 16th June 2008, 09:15
  5. Use/Misuse of Pointers
    By ct in forum General Programming
    Replies: 12
    Last Post: 8th May 2007, 23:43

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.