pointers as arguments

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!
 
O

Oleg Gaier

Hi,
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
 
S

Stefan Ram

crystal twix said:
int *computeMin(const int *a, const int *b) {
return. But I thought you could use constants so the values don't get

»a« is not a constant, but a pointer to const, that is, a
read-only pointer, while you return a read-write-pointer.
So, you try to convert a read-only-permission into a
read-write-permission.

To have constants »a« and »b«, use:

int *computeMin(int * const a, int * const b)

.
 
R

red floyd

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;}

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?
 
C

crystal twix

I'm trying to do it for a class assignment and trying to understand
how the syntax works besides the basic, declaring a pointer, pointing
to an address, and the example of passing in a variable as a reference
to the variable like
swapVariables(int &a, int &b)

so that's what I am trying to understand. Thanks!
 
C

crystal twix

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. 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.
 
J

James Kanze

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);
}

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.
 
N

Nick Keighley

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
 
C

crystal twix

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!
 
B

Brian

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!

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."
 
B

Brian

I haven't used the new operator in class yet.  Is there another
method?


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
 
N

Nick Keighley

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top