Like Tree2Likes

remove warning message multi dimensional pointers

This is a discussion on remove warning message multi dimensional pointers within the C Programming forums, part of the General Programming Boards category; Code: Besides a bit more work - What are the disadvantages of treating a pointer to a 2D array like ...

  1. #16
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Code:
    Besides a bit more work - What are the disadvantages of treating a pointer to a 2D array like a pointer to a 2D array?
    This terminology doesn't work.

    Code:
    int (*)[5] => pointer to array of 5 ints
    
    int (*)[5][5] => pointer to array of 5 arrays of 5 ints
    Which one you use will depend on the situation. Consider the following:

    int a[10];

    In this case you might use one of the following:

    int *b = a;
    int (*c)[10] = &a;

    The main advantage of using a pointer is so that you can perform arithmetic on it. c + 1 will point at the end of a (which can't be dereferenced), whereas b + 1 will point at a[1]. Since it's hardly useful to point at &a + 1, there isn't much reason to use c in this case.

  2. #17
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,421
    Why doesn't that terminology work?
    Fact - Beethoven wrote his first symphony in C

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Click_here
    I'm proposing that by forcing the programmer to dereference a pointer to get its value, you are reminding everyone that it is a pointer. Isn't that a good thing?
    And indeed multip[i][j] dereferences the pointer named multip. The point here is that the conversion of an array to a pointer to its first element is canonical in C. Trying to avoid that by passing a pointer to the array rather than the array itself (and hence a pointer to its first element) is unusual and complicates the syntax unnecessarily.

    If you really want to avoid this, then I would say that the proper thing to do is to encapsulate the 2D array in a struct:
    Code:
    struct click_here_type
    {
        int x[5][5];
    };
    
    void click_here_print_element(const struct click_here_type *click_here, size_t i, size_t j)
    {
        printf("%d", click_here->x[i][j]);
    }
    
    /* ... */
    click_here_type click_here;
    /* ... */
    click_here_print_element(&click_here, 1, 1);
    Quote Originally Posted by Click_here
    What are the disadvantages of treating a pointer to a 2D array like a pointer to a 2D array?
    There are no disadvantages in doing that. Rather, the disadvantage is in having a pointer to a 2D array when a pointer to a 1D array will suffice.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,421
    Quote Originally Posted by laserlight
    Rather, the disadvantage is in having a pointer to a 2D array when a pointer to a 1D array will suffice.
    Just for completeness - Can you think of an advantage to using a pointer to a 2D array?
    Fact - Beethoven wrote his first symphony in C

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Click_here
    Can you think of an advantage to using a pointer to a 2D array?
    Sure, e.g., to iterate over the elements of a 3D array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,421
    Quote Originally Posted by laserlight View Post
    Sure, e.g., to iterate over the elements of a 3D array.
    A good sense of humour is not lost on me :P
    Fact - Beethoven wrote his first symphony in C

  7. #22
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Why doesn't that terminology work?
    Well, with that terminology you call the following three types the same thing.

    Code:
    char (*)[3][1];
    int (*)[4][2];
    int (*)[5][2];

  8. #23
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,421
    Quote Originally Posted by Barney McGrew View Post
    Well, with that terminology you call the following three types the same thing.

    Code:
    char (*)[3][1];
    int (*)[4][2];
    int (*)[5][2];
    A pointer to a 2D array?
    Fact - Beethoven wrote his first symphony in C

  9. #24
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,421
    Quote Originally Posted by Click_here View Post
    A pointer to a 2D array?
    Also, I think that that is fine - Because we are not discussing the size of the 2D arrays, nor the data type.

    What we are discussing is whether using a pointer to a 2D array has any advantages over a pointer to a 1D array.
    Fact - Beethoven wrote his first symphony in C

  10. #25
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    I don't see how it's helpful to talk about imaginary types.

  11. #26
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,421
    Quote Originally Posted by Barney McGrew View Post
    I don't see how it's helpful to talk about imaginary types.
    What do you mean by that?
    Fact - Beethoven wrote his first symphony in C

  12. #27
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,421
    Quote Originally Posted by Barney McGrew View Post
    I don't see how it's helpful to talk about imaginary types.
    Do you mean that a conversation about an int will differ from a char?
    Fact - Beethoven wrote his first symphony in C

  13. #28
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    The thing is that 'pointer to 2D array' doesn't describe a specific C type. Although I see how it can be used to refer to an abstract pointer type which points at an array of an array, I think it's much clearer to talk about actual types (in this case, int (*)[5][5]). This way you're talking about a specific type that won't be confused with a similar one (eg. int (*)[][5]).

    Do you mean that a conversation about an int will differ from a char?
    Well, we're talking about using pointers with an object of type int[5][5]. I think a similar conversation about an object of type char[5][5] would be the same, although you'd be using different types in both conversations.

    In general I find it's less confusing to describe specific types rather than certain characteristics of types.

  14. #29
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,421
    Quote Originally Posted by Barney McGrew View Post
    I find it's less confusing to describe specific types rather than certain characteristics of types.
    Fair enough.
    Fact - Beethoven wrote his first symphony in C

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  2. Dynamic, multi-dimensional pointers!
    By Lionmane in forum C Programming
    Replies: 16
    Last Post: 07-07-2005, 01:43 AM
  3. Pointers to multi dimensional arrays.
    By bartybasher in forum C++ Programming
    Replies: 2
    Last Post: 08-25-2003, 02:41 PM
  4. Array pointers to Multi-Dimensional Arrays
    By MethodMan in forum C Programming
    Replies: 3
    Last Post: 03-18-2003, 08:53 PM
  5. Hello all and pointers to multi-dimensional arrays
    By Mario in forum C++ Programming
    Replies: 16
    Last Post: 05-17-2002, 08:05 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21