# remove warning message multi dimensional pointers

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-29-2012
sandbucket
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?
• 11-29-2012
qny
By declaring multip as a pointer to arrays of 5 ints

Code:

```    int multid[5][5];     int (*multip)[5];     multip = multid;```
• 11-29-2012
Click_here
Quote:

Originally Posted by qny
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');     }```
• 11-29-2012
grumpy
Quote:

Originally Posted by Click_here
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".
• 11-29-2012
Click_here
Quote:

Your comment here is incorrect. The ampersand is required.
Apologies - Yes it is.

Quote:

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.
• 11-29-2012
Click_here
Note that removing the '&' gets rid of the warning.
• 11-29-2012
Click_here
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.
• 11-29-2012
Click_here
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.
• 11-29-2012
grumpy
Quote:

Originally Posted by Click_here
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.
• 11-29-2012
Click_here
Quote:

Originally Posted by grumpy
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.
• 11-29-2012
laserlight
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.
• 11-30-2012
Click_here
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?
• 11-30-2012
grumpy
Quote:

Originally Posted by Click_here
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
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
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.
• 11-30-2012
Click_here
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.
• 11-30-2012
Click_here
Besides a bit more work - What are the disadvantages of treating a pointer to a 2D array like a pointer to a 2D array?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last