### Homework question on pointers

Pages: 12
When I remove the &'s I get a segmentation fault. (edit: maybe not. It did the first couple times, but I closed my editor and console and reopened them and now it's fine. Still wrong, but runs without the addresses in the if statements.) Also, replacing the &'s with *'s (dereferencing) give... wait for it...

SEGMENTATION FAULT.
Last edited on
Now it is returning int b no matter what. If I change the values of a,b,c it returns b. If I change the order the values are passed to the function, it returns b. If I change the order the values are input to the function, it returns b. So the logic is flawed.

 123456789101112131415161718192021222324252627282930313233 #include #include using namespace std; int *fun(int *a, int *b, int *c) { if (a > c && b > a) return a; else if (b > c && a > b) return b; else if (c > b && a > c) return c; } int main(void) { int a = 2; int b = 3; int c = 4; int* pMedian; cout << "Address of a =" << &a << endl; cout << "Address of b =" << &b << endl; cout << "Address of c =" << &c << endl; pMedian= fun(&a, &b, &c); cout << "Address of the Median=" << pMedian<< endl; cout << "The value of the Median=" << *pMedian<< endl; return 1; }
Last edited on
And one more time, cire, thank you very much. I am filled with rage, but I have to solve this to move on. I keep thinking I understand it and I can technically get the correct output, but then I play with it more and it doesn't output what I need.
In the code above you're comparing the addresses held in the pointer variables. You need to use * to dereference the pointers so you can compare what's pointed to, and you need to correct your faulty logic to determine the median.

Consider what happens when *a == 3, *b == 2, and *c == 4.

*a is the median correct?

But according to the logic in your function, *a may only be the median if:

*a>*c && *b>*a

or, if we substitute the actual values in:

3>4 && 2>3

Last edited on
If I create a variable to dereference the pointer into, then I cant pass that back to the main function though.

THIS IS DRIVING ME INSANE. I HATE THIS.
You can simply dereference the pointers.

 1234567891011121314 if ( *a > *c ) { if ( *a < *b ) return a ; if ( *b < *c ) return b ; return c ; } else { // ... }
That does not seem to return a pointer. I don't even know anymore what it returns. I am miserable over this.
int* fun( int* a, int* b, int* c)

a, b and c are pointers. Otherwise *a, *b, and *c would make no sense.
I would like to paypal you \$5 for putting up with me.
Holy *!\$%^&* I think it works:

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 #include #include using namespace std; int *fun(int *a, int *b, int *c) { if (*a > *c ) { if (*a < *b) return a; if (*c > *b) return c; return b; } if (*b > *c) { if (*b < *a) return b; if (*c > *a) return c; return a; } if (*c > *b) { if (*c < *a) return c; if (*b > *a) return b; return a; } } int main(void) { int a = 2; int b = 3; int c = 4; int* pMedian; cout << "Address of a =" << &a << endl; cout << "Address of b =" << &b << endl; cout << "Address of c =" << &c << endl; pMedian= fun(&a, &b, &c); cout << "Address of the Median=" << pMedian<< endl; cout << "The value of the Median=" << *pMedian<< endl; return 1; }
Haha Mac, welcome to pointers man. As a fellow C++ newb I understand your pain, believe me. But once it clicks, it will click HARD. :) Wait until you get to pointers to functions, or better yet, like I had to do with one exercise, create a pointer to an array of pointers to functions that return pointers and take pointers as arguments. Good lord, it was asterisks EVERYWHERE.

Anyway, if it helps you to think of it differently remember you can attach the asterisk to the TYPE instead of the variable name, like this: int* a; This declares a pointer to int and names it a just the same as int *a; Remember the asterisk in the declaration is a TYPE, and that type is pointer, but you also have to include the type the pointer points to, which is int. After you declare a variable you don't include it's type any time you use it. Which is why saying return a; is returning a pointer. Saying return *a; return the value that a pointed to because it include the dereference operator. Honestly, I hoped one of the thing they would do with the C++11 standard was add a unique operator for dereferencing because I think it is what causes most of the confusion with pointers.

Now the reason you have to declare your function as int* fun(int* a, int* b, int* c) in the declaration is because you HAVE to include each argument and return type. Remember, each is type POINTER TO INT. After the declaration, within the function, you no longer refer to each by type and name, but solely by name, IE, a, b and c. And which is why you return a, which is type pointer to int, just like you declared.

I know it's frustrating, and I have seen some pretty good analogies for explaining pointers a bit better. If you still need help wrapping your head around them don't worry. It's prolly one of the harder, but also rather important, points to get about C++ from what I'm told.

BTW, here is the declaration of that pointer I was talking about:
const double *(*(*pd)[3])(const double*, int)
Last edited on
If I can throw a curve ball .......

Wouldn't a more general solution be to sort all the values, then you can easily use the array subscript to calculate the median? I am not sure whether anyone mentioned that already - it was too long ago......

One thing about references - they are very similar to pointers, but they are safer because a reference is always associated with a variable - this can help avoid some dangers with pointers. I know your assignment requires pointers.

BTW - you haven't done any pointer arithmetic yet.......

Here is a link to K&R C programming - it has a whole chapeter on pointers & arrays - worth reading.