int *computeMin(const int *a, const int *b) {
//is this wrong?
return *a < *b ? *a : *b;
}
(I get a compiler error saying I cannot convert const int to int in
return. But I thought you could use constants so the values don't get
changed, and just return the pointer to that value. So in the above
function, I am not sure if that would be the correct way to do it
given my function declaration. I thought that would be correct since
* means the value of, and I thought I could just compare the values to
get the minimum).
Then in my main function, I am not sure how I would get inputs and
pass it to the functions.
I thought I would do something like:
int x;
int y;
computeMin(&x, &y);
But this is wrong. So I guess I'm trying to understand what is right
and why it is right, along with, how I would do the same thing as
int &computeMin(const int &x, const int &y)
Any thoughts? Thanks!
> int *computeMin(const int *a, const int *b) {
> //is this wrong?
> return *a < *b ? *a : *b;
> }
The function computeMin gets the pointers a and b as const and you tries
to return the pointer as not const. This is wrong. You can write :
const int* computeMin( const int *a, const int *b )
......
> int &computeMin(const int &x, const int &y)
The same mistake as above.
Greetings
What are you doing wrong? You mean aside from the fact that your
code returns an int, and not an int*?
Why are you passing ints by adddress?
so that's what I am trying to understand. Thanks!
On Oct 23, 2:43 pm, red floyd <redfl...@gmail.com> wrote:
> On Oct 23, 9:43 am, 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
double *computeMaximum(const double *a, const double *b) {
return((double *) *a > *b ? *a : *b);
}
I would think that in my return statement I am using *a and *b to
represent the values of a and b, and depending on the value of a and
b, I return the greater of the two. In my conditional operator, I
return the value of a or value b with the notation *a and *b. So this
is type double, but I need to return a pointer to a double so I try
casting it as seen in my example, but I still get the problem:
Invalid cast from type 'double' to type 'double *'
Am I on the right track? Thanks.
> Can I ask if I understand this correctly for my example?
>
> double *computeMaximum(const double *a, const double *b) {
> return((double *) *a > *b ? *a : *b);
> }
>
> I would think that in my return statement I am using *a and *b to
> represent the values of a and b, and depending on the value of a and
> b, I return the greater of the two.
Right. Except that you are returning a pointer to double, so you cannot
literally return "the greater of the two" .
> In my conditional operator, I
> return the value of a or value b with the notation *a and *b. So this
> is type double, but I need to return a pointer to a double so I try
> casting it as seen in my example, but I still get the problem:
> Invalid cast from type 'double' to type 'double *'
Yes, a double is not the same as a pointer to a double. Besides, you
apply the cast only to the first *a, you should have had some parens
there, but C-style casting should be best avoided anyway in C++ code.
You can return a pointer instead if this is what you really want:
return( &(*a > *b ? *a : *b));
or more simply:
return(*a > *b ? a : b);
However, be warned that such operating on single doubles via pointers is
something which you don't see often (hopefully) in real code. It just
complicates the code with no real benefit. As an exercise this is fine,
of course.
hth
Paavo
> double *computeMaximum(const double *a, const double *b) {
> return((double *) *a > *b ? *a : *b);
> }
No. As someone else has already said, you definitely shouldn't
be using pointers here. But a double is *not* a pointer to
double, and there are no conversions between doubles and
pointers. If for some unknown reason you did need to use
pointers here, then you must return one of the pointers, e.g.:
double* computeMaximum(double* a, double* b)
{
return *a > *b ? a : b;
}
> I would think that in my return statement I am using *a and *b
> to represent the values of a and b,
You are using *a and *b to represent the values pointed to be a
and b.
> and depending on the value of a and b, I return the greater of
> the two.
No. Depending on the value of what a and b point to, you
return... I've not yet figured out what you're trying to
return.
> In my conditional operator, I return the value of a or value b
> with the notation *a and *b. So this is type double, but I
> need to return a pointer to a double so I try casting it as
> seen in my example, but I still get the problem: Invalid cast
> from type 'double' to type 'double *'
Once you have the pointed to value, you can't go back to the
address at which it was found. You have to use the original
address.
--
James Kanze
> I'm trying to understand how pointers and reference variables work in
> functions as arguments and 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 using pointers incorrectly
2. pointers are 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 use
pointers here (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 its arguments).
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 need pointers as arguments in 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 C pointers are often not a good
idea even then and you should seriously look at things called "smart
pointers". But your course may not have got that far yet!
Dig out the C++ FAQ and see what that has to say about pointers and
references.
nick keighley
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 usepointershere (you don't, I assure you). You'd write it.
> 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 "smartpointers". But your course may not have got that far yet!
You're not allocating memory for the pointers
to point to.
int* x = new int;
int* y = new int;
Brian Wood
http://webEbenezer.net
"The kingdom of heaven is like a treasure hidden
in the field, which a man found and hid again;
and from joy over it he goes and sells all that
he has and buys that field."
On Oct 26, 7:05 pm, Brian <c...@mailvault.com> wrote:
> On Oct 26, 8:23 pm, crystal twix <jonwongfanc...@gmail.com> wrote:
>
>
>
>
>
> > 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 twopointersin 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!
>
> You're not allocating memory for thepointers
> to point to.
>
> int* x = new int;
> int* y = new int;
>
> Brian Woodhttp://webEbenezer.net
double x = 0;
double y = 0;
cin >> x;
cin >> y;
double* res = computeMaximum(&x, &y);
But others have already been over this so I'm
not sure if you're paying attention.
Brian Wood
http://webEbenezer.net
And I will make your descendants to multiply as the stars
of heaven, and will give unto your descendants all these
lands; and by your descendants all the nations of the
earth shall be blessed; Genesis 26:4
re-read my previous post.
> 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));
>
> }
What is the cast for?
> Now I am trying to figure out how to actually call it.
I gave examples.
> My initial guess would be to declare two pointers in main
>
> int *x;
> int *y;
what do x and y point at? What type are they?
<snip>