443,432 Members | 795 Online Need help? Post your question and get tips & solutions from a community of 443,432 IT Pros & Developers. It's quick & easy.

# Pointer to matrix example

 P: n/a Wrong one: void minptr (int *matrix, int rows, int columns,int *min){ int i=0,j=0; *min=*matrix; //!!!!!!!!!!!!!!!!! for (i=0; i < rows; i++) { for (j=0; j < columns; j++) { if( *min *(matrix+(i*columns)+j) ) { min = (matrix+(i*columns)+j); } } } } Right one: int* minptr2 (int *matrix, int rows, int columns){ int i=0,j=0; int *min=matrix; for (i=0; i < rows; i++) { for (j=0; j < columns; j++) { if( *min *(matrix+(i*columns)+j) ) { min = (matrix+(i*columns)+j); } } } return min; }; These functions are supposed to give the pointer to the minimal element of a 3x4 matrix. It is claimed that the first one is wrong because it calls "min" by value. Can anyone make it clear why the first one is wrong and the second one is right? Plus, inthe second one, min points to a pointer (matrix) how can the line *min *(matrix+(i*columns)+j) make sense then? How can you confront the value of a pointer with the value in the matrix? Jul 18 '07 #1
5 Replies

 P: n/a Ohh... thanks in advance. Jul 18 '07 #2

 P: n/a Anolethron wrote On 07/18/07 13:32,: Wrong one: void minptr (int *matrix, int rows, int columns,int *min){ int i=0,j=0; *min=*matrix; //!!!!!!!!!!!!!!!!! for (i=0; i < rows; i++) { for (j=0; j < columns; j++) { if( *min *(matrix+(i*columns)+j) ) { min = (matrix+(i*columns)+j); } } } } Right one: int* minptr2 (int *matrix, int rows, int columns){ int i=0,j=0; int *min=matrix; for (i=0; i < rows; i++) { for (j=0; j < columns; j++) { if( *min *(matrix+(i*columns)+j) ) { min = (matrix+(i*columns)+j); } } } return min; }; These functions are supposed to give the pointer to the minimal element of a 3x4 matrix. It is claimed that the first one is wrong because it calls "min" by value. Can anyone make it clear why the first one is wrong and the second one is right? The first version would be called like this: int mat = ...; int minimum; minptr(&mat, 3, 4, &minimum); However, the only "externally visible" effect of the call would be the same as if you had written minimum = mat; The line with all the exclamation points copies the matrix' first value to the variable that min points to -- that is, to minimum in the caller. Thereafter, nothing that happens inside minptr has any effect on the caller. When a function changes the value of one of its own arguments, it's only changing its own local copy of that value, so all the adjustments to `min' that go on inside the function remain unseen outside. The second function sets up and probably makes changes to its own local variables, and these changes are also invisible from outside. However, the second function returns a copy of one of those variables as its own function value. You would call it like this: int mat = ...; int *smallest; smallest = minptr2(&mat, 3, 4); The caller can't actually see the variable min inside minptr2 -- in fact, by the time minptr2 returns, min doesn't even exist any more -- but the value that was stored in that variable is now stored in smallest because it was returned as the function value. Plus, inthe second one, min points to a pointer (matrix) how can the line *min *(matrix+(i*columns)+j) make sense then? How can you confront the value of a pointer with the value in the matrix? (I think that by "confront" you mean what a native English speaker would call "compare.") Notice the `*' operators at the start of the expression on each side of the comparison. Simplified, it looks like *a_pointer *another_pointer .... and this doesn't compare the pointers, but the things that they point to. `min' points to some element of the matrix, `(matrix+(i*columns)+j)' points to a different element (except on the very first time through the loop, when they both point to the  element), and the comparison checks whether one element is larger than the other. The pointers just indicate where the things to be compared are; the pointers themselves are not compared. -- Er*********@sun.com Jul 18 '07 #3

 P: n/a Eric Sosman ha scritto: >Wrong one: void minptr (int *matrix, int rows, int columns,int *min){ int i=0,j=0; *min=*matrix; //!!!!!!!!!!!!!!!!! for (i=0; i < rows; i++) { for (j=0; j < columns; j++) { if( *min *(matrix+(i*columns)+j) ) { min = (matrix+(i*columns)+j); } } }} The first version would be called like this: int mat = ...; int minimum; minptr(&mat, 3, 4, &minimum); However, the only "externally visible" effect of the call would be the same as if you had written minimum = mat; The line with all the exclamation points copies the matrix' first value to the variable that min points to -- that is, to minimum in the caller. Thereafter, nothing that happens inside minptr has any effect on the caller. When a function changes the value of one of its own arguments, it's only changing its own local copy of that value, so all the adjustments to `min' that go on inside the function remain unseen outside. Is this because '*min' is an argument of the function while 'min' is not? Jul 18 '07 #4

 P: n/a Anolethron wrote On 07/18/07 17:20,: Eric Sosman ha scritto: >>>Wrong one: void minptr (int *matrix, int rows, int columns,int *min){ int i=0,j=0; *min=*matrix; //!!!!!!!!!!!!!!!!! for (i=0; i < rows; i++) { for (j=0; j < columns; j++) { if( *min *(matrix+(i*columns)+j) ) { min = (matrix+(i*columns)+j); } } }} The first version would be called like this: int mat = ...; int minimum; minptr(&mat, 3, 4, &minimum);However, the only "externally visible" effect of thecall would be the same as if you had written minimum = mat;The line with all the exclamation points copies thematrix' first value to the variable that min pointsto -- that is, to minimum in the caller. Thereafter,nothing that happens inside minptr has any effect onthe caller. When a function changes the value of oneof its own arguments, it's only changing its own localcopy of that value, so all the adjustments to `min'that go on inside the function remain unseen outside. Is this because '*min' is an argument of the function while 'min' is not? No; `min' is the argument, and its type is `int *'. C is a "call by value" language, meaning that the function receives its own private copy of the argument values provided by the caller. The effect is as if `min' were just a local variable in the function, initialized magically with the fourth value in the caller's list of arguments. Once it starts executing, the function can change the value of `min' if it likes -- but it's only changing the value of one of its own variables, and it's not changing anything in the caller. Here's a simple example to show why this policy makes sense. Consider the following tiny program: #include void plus_one(int x) { x = x + 1; printf ("x = %d\n", x); } int main(void) { int y = 42; plus_one (y); printf ("y = %d\n", y); return 0; } What do you think the output should be? We start with y equal to 42, we pass it to plus_one under the alias x, it gets incremented to 43 and gets printed, then we come back to main and print 43 again, right? No: the change to x does not alter y; y is still 42. (Try it!) Now for the "makes sense" part. Add the following lines to main, just before the return statement: plus_one (y / 6); /* should y increase by 6? */ plus_one (0); /* should 0 change to 1? */ The plus_one function does *not* somehow reach back into main and change the value of y so that y/6 becomes one greater than it was. The plus_one function does *not* reach back into main and change the value of the "constant" zero. Argument-list communication is one-way: from the caller to the called function, without any feedback. (Of course, when the argument value is a pointer, the function can use the pointer to change the thing it points to. But that's not really the same thing at all; the value passed by the caller cannot be changed, even if the value enables the function to alter some of the terms in the expression that produced the value.) -- Er*********@sun.com Jul 18 '07 #5

 P: n/a Eric Sosman ha scritto: > No; `min' is the argument, and its type is `int *'. Sorry, that's exactly what I meant, I just wrote something else. Jul 19 '07 #6

### This discussion thread is closed

Replies have been disabled for this discussion. 