Thread: remove warning message multi dimensional pointers

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    8

    remove warning message multi dimensional pointers

    Code:
    #include <stdio.h>
    
    int main()
    {
        int multid[5][5];
        int **multip;
        
        multip = multid;
        
        printf("%d", multip[1][1]);
        
        return 0;
    }
    here is my code. how can i remove the warning message?

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    By declaring multip as a pointer to arrays of 5 ints

    Code:
        int multid[5][5];
        int (*multip)[5];
    
        multip = multid;

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by qny View Post
    By declaring multip as a pointer to arrays of 5 ints

    Code:
        int multid[5][5];
        int (*multip)[5];
    
        multip = multid;
    Or better yet, a pointer to a 2D array with 5x5 elements...
    Code:
       int multid[5][5] =  {
                                {0, 1, 2, 3, 4} ,       
                                {5, 6, 7, 8, 9},
                                {10, 11, 12, 13, 14},   
                                {15, 16, 17, 18, 19},
                                {20, 21, 22, 23, 24}
                            };
    
    
        int (*multip)[5][5];
        
        int i,j;
    
    
        multip = &multid; /* Note that '&' is not needed,
                                    but it gives someone who is reading
                                    your code a clue */
    
    
        for (i=0; i<5; i++)
        {
            for (j=0; j<5; j++)
            {
                printf("%d ", (*multip)[i][j]);
            }
    
    
            putchar('\n');
        }
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Click_here View Post
    Or better yet, a pointer to a 2D array with 5x5 elements...
    Code:
       int multid[5][5] =  {
                                {0, 1, 2, 3, 4} ,       
                                {5, 6, 7, 8, 9},
                                {10, 11, 12, 13, 14},   
                                {15, 16, 17, 18, 19},
                                {20, 21, 22, 23, 24}
                            };
    
    
        int (*multip)[5][5];
        
        int i,j;
    
    
        multip = &multid; /* Note that '&' is not needed,
                                    but it gives someone who is reading
                                    your code a clue */
    Your comment here is incorrect. The ampersand is required.

    Would you care to provide an example to illustrate your claim that a pointer to 2D array with 5x5 elements is really "better".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Your comment here is incorrect. The ampersand is required.
    Apologies - Yes it is.


    Would you care to provide an example to illustrate your claim that a pointer to 2D array with 5x5 elements is really "better"
    Because this results in a warning
    C:\Project Euler\Sketch\main.c||In function 'main':|
    C:\Project Euler\Sketch\main.c|26|warning: assignment from incompatible pointer type|
    ||=== Build finished: 0 errors, 1 warnings ===|
    Code:
      int multid[5][5] =  {
                                {0, 1, 2, 3, 4} ,
                                {5, 6, 7, 8, 9},
                                {10, 11, 12, 13, 14},
                                {15, 16, 17, 18, 19},
                                {20, 21, 22, 23, 24}
                            };
    
    
    
    
        int (*multip)[5];
    
    
        int i,j;
    
    
        multip = &multid;
    
    
        for (i=0; i<5; i++)
        {
            for (j=0; j<5; j++)
            {
                printf("%d ", multip[i][j]);
            }
    
    
    
    
            putchar('\n');
        }

    Which would make sense -> you have declared multip as a pointer to an array of 5 elements, not a 2D array.
    Fact - Beethoven wrote his first symphony in C

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Note that removing the '&' gets rid of the warning.
    Fact - Beethoven wrote his first symphony in C

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I think that the
    Code:
    int (*multip)[5][5];
    ...is better - Because it is more descriptive.

    It defines multip as a pointer to an array of 5 to an array of 5

    It is a pointer to a 2d array by definition.
    Fact - Beethoven wrote his first symphony in C

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    And to ask me to provide an example where it is "better" is asking for my opinion as fact.

    I think that it is always better to treat it exactly as it is intended -> A pointer (can be seen with the dereferencing throughout the code) to a 2D array. You may say that the dereferencing is a disadvantage, but I disagree - It tells the reader that they are looking at a pointer to an array, and not the array itself.
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Click_here View Post
    And to ask me to provide an example where it is "better" is asking for my opinion as fact.
    I ask for you for an example to clarify your opinion, and reason for it, no more. I was seeking to understand why you consider it better, not to shoot your opinion down.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by grumpy View Post
    I ask for you for an example to clarify your opinion, and reason for it, no more. I was seeking to understand why you consider it better, not to shoot your opinion down.
    I suppose the best way to explain why I think it is better is using the code from before
    Code:
    int multid[5][5] =  {
                                {0, 1, 2, 3, 4} ,       
                                {5, 6, 7, 8, 9},
                                {10, 11, 12, 13, 14},   
                                {15, 16, 17, 18, 19},
                                {20, 21, 22, 23, 24}
                            };
    
    
    
    
        int (*multip)[5][5];
        
        int i,j;
    
    
    
    
        multip = &multid; 
    
        for (i=0; i<5; i++)
        {
            for (j=0; j<5; j++)
            {
                printf("%d ", (*multip)[i][j]);
            }
    
    
    
    
            putchar('\n');
        }
    When used in code, it is clear to see that multip is a pointer and not an array. This is important, because although arrays and pointers are similar, they are not the same.
    Fact - Beethoven wrote his first symphony in C

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Click_here
    When used in code, it is clear to see that multip is a pointer and not an array. This is important, because although arrays and pointers are similar, they are not the same.
    I don't see the point of adding an unnecessary level of indirection. In my opinion, multip[i][j] is (slightly) more readable than (*multip)[i][j]. Furthermore, the expression (*multip)[i][j] can be used when multip is an array (of appropriate type), hence the the claim that it makes it clear that multip is a pointer and not an array is false.

    That said, I note that sandbucket did not state what is the point of this exercise, so your (corrected) answer is equally valid, as is:
    Code:
    #include <stdio.h>
     
    int main(void)
    {
        int multid[5][5];
        /* code to populate multid? ... */
    
        printf("%d", multid[1][1]);
    
        return 0;
    }
    That is, getting rid of "remove warning message multi dimensional pointers" by getting rid of the pointer variable to begin with since it is unnecessary.
    Last edited by laserlight; 11-29-2012 at 11:36 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Hypothetically: If you had a pointer to an int, and you could (somehow) create a way of using it where you didn't have to dereference - Would you want to?

    I don't mean create another variable and use that other variable -> I mean the pointer.

    A consequence for this is that the variable may not always point to the same location, and the variable has no clues that it is a pointer.

    I want to stress that this is a hypothetical situation, as it can't actually be done.

    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?
    Fact - Beethoven wrote his first symphony in C

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Click_here View Post
    Hypothetically: If you had a pointer to an int, and you could (somehow) create a way of using it where you didn't have to dereference - Would you want to?
    By definition, no. The only usages of pointers that don't involve dereferencing are retrieving or setting the value of the pointer (i.e. affecting the stored address).

    Quote Originally Posted by Click_here View Post
    A consequence for this is that the variable may not always point to the same location, and the variable has no clues that it is a pointer.
    There are no variables that behave differently if they are being pointed to by a pointer.


    Quote Originally Posted by Click_here View Post
    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?
    The "good things" associated with any language construct or with any variable is in what can be achieved using them. The ability to use something does not require knowing every detail of what it is.

    I can drive my car without knowing anything at all about the software in its fuel control computer. Knowing the internal details of that software would not make me a safer driver.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    It was a hypothetical, so I'm not going to dwell on it too much.

    Also, I don't think that a car is a good comparison. Pointers and non-pointers need to be "driven" differently -> Like driving a car compared to a motorcycle.
    Fact - Beethoven wrote his first symphony in C

  15. #15
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Besides a bit more work - What are the disadvantages of treating a pointer to a 2D array like a pointer to a 2D array?
    Fact - Beethoven wrote his first symphony in C

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, 04: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, 09: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