C
crystal twix
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?
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!
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?
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!