5 Replies - 255 Views - Last Post: 09 August 2012 - 02:21 PM Rate Topic: -----

#1 JamesFlanigan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-August 12

Pointers about Pointers to Constants

Posted 08 August 2012 - 11:57 PM

Howdy Yall,

I am trying to do an exercise that will evaluate 2 numbers and return the largest.
However, the function that evaluates and returns the largest number can only have 2 parameters.
Both of which are pointers to cont double, yet the function is to return a pointer to double, not const double.

I have done something similar before and am supposed to do the evaluation using the ternary operator.
However I dont fully understand how pointers work.

Any Pointers about Pointers?

here is my code

#include <cstdlib>
#include <iostream>

using namespace std;

double ComputeMaximum(double *num1, double *num2)
{
    return((*num1 < *num2) ? *num2 : *num1);
}
int main()
{
    const double numToEval_1 = 6.9;
    const double numToEval_2 = 6.5;
    double *pointToNum1 = numToEval_1;
    double *pointToNum1 = numToEval_2;
    computeMaximum(*numToEval_1, *numToEval_2);
    cout << "%f", num1
    system("PAUSE");
    return EXIT_SUCCESS;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Pointers about Pointers to Constants

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: Pointers about Pointers to Constants

Posted 09 August 2012 - 01:11 AM

> 06 double ComputeMaximum(double *num1, double *num2)
This should be
double ComputeMaximum(const double *num1, const double *num2)


14 double *pointToNum1 = numToEval_1;
15 double *pointToNum1 = numToEval_2;
16 computeMaximum(*numToEval_1, *numToEval_2);
17 cout << "%f", num1
This you can simplify to
cout << computeMaximum(&numToEval_1, &numToEval_2);


> Both of which are pointers to cont double, yet the function is to return a pointer to double, not const double.
At the moment, the code returns the double by value, which is fine.

Returning a non-const double pointer, when all you're given is const double pointers is a whole "can of worms" you don't want to open at this stage. There are several ways of doing this, but none are as simple and clean as either returning by value (as now), or returning a const double pointer (ie, one of the input parameters).

This is a good pointer tutorial
http://pw1.netcom.co...tr/pointers.htm
It's aimed at C, but the general idea of pointers is common in both languages.

C++ has references, and STL Containers which largely do away with the need to use pointers at all in C++ code. For the most part, you only really need them when interfacing with legacy C API's.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4884
  • View blog
  • Posts: 11,280
  • Joined: 16-October 07

Re: Pointers about Pointers to Constants

Posted 09 August 2012 - 06:02 AM

#include <cstdlib>
#include <iostream>

using namespace std;

// this is fine, if silly, and perhaps misleading
// there's no real point to pointers here; only as an exercise
double ComputeMaximum(double *num1, double *num2) {
    return((*num1 < *num2) ? *num2 : *num1);
}


int main() {
    const double numToEval_1 = 6.9;
    const double numToEval_2 = 6.5;
    // whoa numToEval_1 is const double, not double *
    // the next two statements should give a compiler error
    double *pointToNum1 = numToEval_1;
    double *pointToNum1 = numToEval_2;
    
    // right, you now ignore those above values, assuming they've worked
    // and pass... what?  What does *numToEval_1 mean?
    computeMaximum(*numToEval_1, *numToEval_2);
    
    // your function returns a value, which you do not capture
    
    // you have not declared num1 here
    // nor would it print, anyway
    // your syntax is in no way correct
    cout << "%f", num1




Try to compile it. If it doesn't compile, look at the errors you're getting and fix them. You haven't even gotten that far.
Was This Post Helpful? 0
  • +
  • -

#4 JamesFlanigan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-August 12

Re: Pointers about Pointers to Constants

Posted 09 August 2012 - 12:12 PM

View Postbaavgai, on 09 August 2012 - 06:02 AM, said:

#include <cstdlib>
#include <iostream>


int main() {
    const double numToEval_1 = 6.9;
    const double numToEval_2 = 6.5;
    // whoa numToEval_1 is const double, not double *
    // the next two statements should give a compiler error
    double *pointToNum1 = numToEval_1;
    double *pointToNum1 = numToEval_2;
    

Thank you for your reply.
Please bear with me this is my first programming class.

Let me take it from the start
So I am supposed to take 2 const doubles and send them as parameters to the function.

i understand that * makes a pointer so if I do

int p=2;
int *e = p;



This would make e pointer to p, correct?
I also understan that I use the & if i want the address of .
So if I go
int p=2;
int *e = &p;



this would make e a pointer to the address of p not the value, correct?

I am unsure however if i need to keep using the * every time I reference pointer e.
Also Do I need the address of anything? Cant i just pass them as poiters?

Is this code correct in the fact that I simply need to use it to make 2 pointers to const double
    const double numToEval_1 = 6.9;
    const double numToEval_2 = 6.5;
    const double *pointToNum1 = &numToEval_1;  //It wont let me do this without putting the & in front of numToEval_1
    const double *pointToNum2 = &numToEval_2;
    computeMaximum(const double *pointToNum1, const double *pointToNum2); //Am I supposed to use the * when passing?


Was This Post Helpful? 0
  • +
  • -

#5 JamesFlanigan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-August 12

Re: Pointers about Pointers to Constants

Posted 09 August 2012 - 12:26 PM

View PostSalem_c, on 09 August 2012 - 01:11 AM, said:

> 06 double ComputeMaximum(double *num1, double *num2)
This should be
double ComputeMaximum(const double *num1, const double *num2)


14 double *pointToNum1 = numToEval_1;
15 double *pointToNum1 = numToEval_2;
16 computeMaximum(*numToEval_1, *numToEval_2);
17 cout << "%f", num1
This you can simplify to
cout << computeMaximum(&numToEval_1, &numToEval_2);


Thanks for your reply,

So I have my program working but dont really know how it works and I dont think it is changing the const doubles to just plain old doubles.

I also am unsure as to why you had me use the & operator in passing the parameters.

It worked but i thought that it passed the adress?

Is it because I used the dereference operator once passed in the *num1 for example?

So I passed the adress by doing

&numToEval_1

but had the program look at the value of that adress when it recieved it in the ComputeMaximum fucntion by doing this
*num1


Here is my code.
#include <cstdlib>
#include <iostream>

using namespace std;

double ComputeMaximum(const double *num1, const double *num2)
{
    return((*num1 < *num2) ? *num2 : *num1);
}
int main()
{
    const double numToEval_1 = 6.4;
    const double numToEval_2 = 6.5;
    const double *pointToNum1;
    const double *pointToNum2;
    
    cout << ComputeMaximum(&numToEval_1, &numToEval_2);
    system("PAUSE");
    return EXIT_SUCCESS;
}


Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 978
  • View blog
  • Posts: 3,395
  • Joined: 19-February 09

Re: Pointers about Pointers to Constants

Posted 09 August 2012 - 02:21 PM

Some operators such as * have different meanings in different contexts.

Here I look at some aspects of pointers. Variables and constants exist in memory and have an memory address.


#include <iostream>
using namespace std;


int main()
{
  // create a constant
  const double number = 6.4;

  // can you assign to a constant ?
  //number = 10;

  // number value and address
  cout << "number=" << number << endl;
  cout << hex << uppercase;
  cout << "address of number=" 
       << &number << endl;

  // create a pointer to a constant
  // What happens if const is removed?
  const double *pointer;

  cout << "uinitialised pointer=" 
       << pointer << endl;

  pointer = 0;
  cout << "nullified pointer=" 
       << pointer << endl;

  pointer = &number;
  cout << "pointer holding address="  
       << pointer << endl;

  // Can you assign to a constant using a pointer?
  //*pointer = 10;
  
  cin.get();
  return(0);
}




Pointers

This post has been edited by #define: 09 August 2012 - 02:23 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1