473,569 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer to matrix example


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*col umns)+j) ) {
min = (matrix+(i*colu mns)+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*col umns)+j) ) {
min = (matrix+(i*colu mns)+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*col umns)+j) make
sense then? How can you confront the value of a pointer with the value
in the matrix?
Jul 18 '07 #1
5 9622
Ohh... thanks in advance.
Jul 18 '07 #2
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*col umns)+j) ) {
min = (matrix+(i*colu mns)+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*col umns)+j) ) {
min = (matrix+(i*colu mns)+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[3][4] = ...;
int minimum;
minptr(&mat[0][0], 3, 4, &minimum);

However, the only "externally visible" effect of the
call would be the same as if you had written

minimum = mat[0][0];

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[3][4] = ...;
int *smallest;
smallest = minptr2(&mat[0][0], 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*col umns)+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_pointe r

.... and this doesn't compare the pointers, but the things
that they point to. `min' points to some element of the
matrix, `(matrix+(i*col umns)+j)' points to a different
element (except on the very first time through the loop,
when they both point to the [0][0] 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
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*col umns)+j) ) {
min = (matrix+(i*colu mns)+j);
}
}

}
}

The first version would be called like this:

int mat[3][4] = ...;
int minimum;
minptr(&mat[0][0], 3, 4, &minimum);

However, the only "externally visible" effect of the
call would be the same as if you had written

minimum = mat[0][0];

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
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*col umns)+j) ) {
min = (matrix+(i*colu mns)+j);
}
}

}
}

The first version would be called like this:

int mat[3][4] = ...;
int minimum;
minptr(&mat[0][0], 3, 4, &minimum);

However, the only "externally visible" effect of the
call would be the same as if you had written

minimum = mat[0][0];

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?
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 <stdio.h>

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4284
by: Peter _Grafenberger | last post by:
hi, i've some troubles compiling Bjarne Strostrup's Matrix class example http://www.research.att.com/~bs/matrix.c under ms visual c++ 6.0 (sp4). i get a lot off errors like matrix_klasse_by_stroustup.cxx(37) : error C2143: Syntaxfehler : Missing ';' before '<'
4
2673
by: Kobu | last post by:
I've read the FAQ and several posts on multidimensional arrays and how their names decay to pointer to arrays (not pointer to pointers). If this is so, why does the following code fragment compiler and run correctly?: #include <stdio.h> int main()
7
2038
by: Marcelo | last post by:
Hi everybody, I don't understand why I am having a problem in this code. The problem is that my pointer *phist in main method, it is declared. Then I send the pointer to my method, and this method creates a new object (a Matrix) for it. I suppose that after the new operator, my pointer is pointing to an object, so when the method has...
8
2461
by: Klaas Vantournhout | last post by:
Hi all, I'm in need of a matrix of function pointers, and to be honest. No 'nice' solution has been found yet on that big big internet. It is possible to declare a matrix of function pointers in the following way void (*f)(int);
7
2720
by: check.checkta | last post by:
Hi, I'd like to implement a simple matrix class. I'd like to overload operator so that it returns as a vector (either the stl vector or some other Vector class of my own). The reason I want to do this is because it enables me to apply some functions of a vector to a row of of matrix, e.g., I have a function to compute the sum of vector:...
4
5531
by: Mark | last post by:
hi, I need some help declaring a matrix of pointers. I made an image to illustrate what I had in mind... http://www.sfu.ca/~mnb2/matrix.gif and here's the relevant code I'm working with (doesn't work) class Tile { public:
1
1461
by: glitchized | last post by:
hi, can anyone help me with matrices multiplication function. how do i go about doing it if i wanna do multiplication of 2 square matrices? i'll first take in the size of the matrix example: cin >> 3 is there any solution/help to do a dynamic multiplication??
2
2570
by: Encrypted | last post by:
i am working with a matrix manipulation program...consists of a matrix class and its member functions.. i have also overloaded << and >>...so dat dey can read and print d whole matrix at one statement.. the code of these overloaded operators is something like this.. // for >> (cin) : istream& operator >> (istream &read, matrix &mat) {...
5
4297
by: Charlie | last post by:
Hi All, I am new to using swig/C++/python. I got some problem with function pointers. I posted in swig-user, but got no response. So I forwarded it here. You help is greatly appreciated. Thanks! ---------- Forwarded message ----------
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.