here is my code. how can i remove the warning message?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?Code:#include <stdio.h> int main() { int multid[5][5]; int **multip; multip = multid; printf("%d", multip[1][1]); return 0; }
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
Apologies - Yes it is.Your comment here is incorrect. The ampersand is required.
Because this results in a warningWould you care to provide an example to illustrate your claim that a pointer to 2D array with 5x5 elements is really "better"
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
Note that removing the '&' gets rid of the warning.
Fact - Beethoven wrote his first symphony in C
I think that the
...is better - Because it is more descriptive.Code:int (*multip)[5][5];
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
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
I suppose the best way to explain why I think it is better is using the code from before
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.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'); }
Fact - Beethoven wrote his first symphony in C
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.Originally Posted by Click_here
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:
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.Code:#include <stdio.h> int main(void) { int multid[5][5]; /* code to populate multid? ... */ printf("%d", multid[1][1]); return 0; }
Last edited by laserlight; 11-29-2012 at 11:36 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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).
There are no variables that behave differently if they are being pointed to by a pointer.
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.
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
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