Re: pointers as arguments
Ok, I seem to be understanding the way you would set up the
function.
double* computeMaximum(const double* a, const double* b) {
return((double *)(*a > *b ? a : b));
}
Now I am trying to figure out how to actually call it. My initial
guess would be to declare two pointers in main
int *x;
int *y;
and then use cin to get the value from the console:
cin >> x;
cin >> y;
and then to call the function
computeMax(x, y)
But I get the error No Match for 'operator>>' in 'std::cin>>x' and one
similarly for y.
Then I tried saying
cin >> *x;
cin >> *y;
which doesn't make sense to me since that looks to me like I'm trying
to set the value of x, rather than pointer to int x to the value of
cin.
That version compiles however, and I get Segmentation Fault when I try
to put in two numbers. I looked that up on Wikipedia and it says that
may happen if I'm trying to access certain values in memory I
shouldn't be. Now I'm even more confused as what is going on. Any
guidance would be great. Thanks!
On Oct 25, 5:31 am, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
On 23 Oct, 16:43, crystal twix <jonwongfanc...@gmail.com> wrote:
I'm trying to understand howpointersand reference variables work in
functions asargumentsand the return type. Like if I want to compute
the minimum with a function like this:
int *computeMin(const int *a, const int *b) {
//is this wrong?
oh, yes... In so many ways.
return *a < *b ? *a : *b;}
1. you are usingpointersincorrectly
2.pointersare only useful in a limited number of places in C
3. these useful places become even more limited in C++
4. you should use templates for a simple generic function like min
5. there's a min() in the standard library
<snip>
I'll only bother with 1-3
1. You've tripped over a common newbie problem with C/C++
declarations. The declaration-mirrors-usage feature. This was either a
brilliant idea by the inventor of C or a complete disaster the
consequences of which we live with to this day.
Consider
int *i;
or int* i; as C++ people tend to write it.
i is an int* or ptr-to-int
and ALSO *i is an int. The declaration mirrors the usage.
Now look at your code. It returns either *a or *b. What type is *a?
It's an int isn't it? And what does your function return? An int* or
ptr-to-int. An int isn't an int* is it? So if you really want to usepoint=
ershere (you don't, I assure you). You'd write it.
int* min (int* a, int* b)
{ return (*a < *b) ? a : b; }
int a, b, c;
c = *min (&a, &b);
But much more sane would be
int min (int a, int b)
{ return (a < b) ? a : b; }
int a, b, c;
c = min (a, b);
2. There are a few places in C where a pointer is useful. One is to
emulate call-by-reference (C has no references). min() is a daft
function to use call by reference (it doesn't modify itsarguments).
But consider swop()
void swop (int* a, int* b)
{
int t;
t = *a;
*b = t;
}
int a,b;
swop (&a, &b);
the function modifies a and b so some sort of reference is needed.
3. but C++ has proper references so even swop() shouldn't be coded
like this. More like
void swop (int& a, int& b)
{
int t;
t = a;
b = t;
}
int a,b;
swop (a, b);
You needpointersasargumentsin C++ much less often. If you want to
pass ownership around or the object may not be present (ie. the
pointer can be NULL) or it can be "reseatable" (the value can change-
references cannot be changed). Raw Cpointersare often not a good
idea even then and you should seriously look at things called "smartpoint=
ers". But your course may not have got that far yet!
Dig out the C++ FAQ and see what that has to say aboutpointersand
references.
nick keighley