# Simple Pointer Problem

• 11-28-2005
Atlas24
Simple Pointer Problem
Hey everybody, I am new to C++, and I have an issue with my program that I cannot figure out.

Code:

```#include <iostream> #include <iomanip> #include <math.h> using namespace std; //function prototype void GetMeasurement(); int GetInt(int iLow, int iHigh); double GetDouble(double, double); double Convert2Metric(int*, double*); void DisplayMetric(int*, double*, double); int iLow=0; int iHigh=0; double dLow=0; double dHigh=0; int main(void) {         int iFeet(0);         double dInches(0.0);         double dMetric(0.0);         int *iDist1=0;         double *dDist2=0;         //use fixed with 3 decimal places throughout the program         cout<< setprecision(3)<<fixed;         //input feet and inches, return using pointers         GetMeasurement();         //convert to metric         dMetric = Convert2Metric(iDist1, dDist2);         //display results as f' and i" = mm meters         DisplayMetric(iDist1, dDist2, dMetric);         system("pause");         return 0; } void GetMeasurement() {         int x=0;         double y=0;         int *iDist1=0;         double *dDist2=0;         cout<<"Measurement in feet: "<<endl;         x=GetInt(0, 1000);         iDist1=&x;         cout<<" "<<endl;         cout<<"Measurement in inches:"<<endl;         y=GetDouble(0.0, 11.999);         dDist2=&y; } int GetInt(int iLow, int iHigh) {         int iDist1=0;         cout<<"Enter a value from 0 to 1000 inclusive: ";         cin>>iDist1;         while(iDist1<iLow || iDist1>iHigh)         {                 cout<<"Measurement in feet:"<<endl<<"Enter a value from 0 to 1000 inclusive: ";                 cin>>iDist1;         }         return iDist1; } double GetDouble(double dLow, double dHigh) {         double dDist2=0;         cout<<"Enter a value from 0.000 to 11.999 inclusive: ";         cin>>dDist2;         while(dDist2<dLow || dDist2>dHigh)         {                 cout<<"Enter a value from 0.000 to 11.999 inclusive: ";                 cin>>dDist2;         }         return dDist2; } double Convert2Metric(int *piDist1, double *pdDist2) {         double dMetric=0;         dMetric= (*piDist1*0.3048)+(*pdDist2*0.254);         return dMetric; } void DisplayMetric(int *piDist1, double *pdDist2, double dMetric) {         cout<<" "<<endl;         cout<<"The length of "<<*piDist1<<"' and "<<*pdDist2<<"'' in metric is "<<dMetric<<" meters."<<endl;         cout<<" "<<endl; }```

The program will compile, but upon entering the Convert2Metric Function, at dMetric= (*piDist1*0.3048)+(*pdDist2*0.254); it will encounter a memory error. Its saying that I am trying to use memory location 0, which is reserved for the system. This is a fairly simple question I am guessing, just something wrong with my pointer syntax?

Help is appreciated.
• 11-28-2005
Atlas24
Ok, I was debugging, stepping through the program, and as it hits that point, the watch window says that my vars *piDist1 and *pdDist2 are indeed at memory location 0. Does anybody know why this is?
• 11-28-2005
Enahs
A pointer points to the memory location.
You need something in that location.
A pointer is not a variable.

Code:

`int *ptr;`
Does not hold anything but a memory address.
You must point it to a variable (of type int in this case)
Code:

```int *ptr,var; p = &var;```
Then
Code:

`*ptr = 5;`
is valid. Or cout/cin *p is valid, ect.

http://www.cprogramming.com/tutorial/lesson6.html
• 11-28-2005
Atlas24
Quote:

Originally Posted by Enahs
A pointer points to the memory location.
You need something in that location.
A pointer is not a variable.

Code:

`int *ptr;`
Does not hold anything but a memory address.
You must point it to a variable (of type int in this case)
Code:

```int *ptr,var; p = &var;```
Then
Code:

`*ptr = 5;`
is valid. Or cout/cin *p is valid, ect.

http://www.cprogramming.com/tutorial/lesson6.html

Yes, I understand this, but aren't I passing that address? * oh, edit, i have read that tutorial, but thanks for the link :)
• 11-28-2005
Enahs
Code:

```        int *iDist1=0;         double *dDist2=0;```
iDist1 and dDist2 has not been pointed to any variable.
You then pass those pointers to your metric function; but those pointers are empty pointers; they point to nutin'.

You create a different set of pointers with the same name in your GetMeas... function. But you do not pass the pointers from main, so the pointers in the GetMeas... function(that you point to something) are only valid in your GetMeas... function despite having the same name.

*edit*
Also, do not use the same variable name over and over, it just makes things confusing.

And put spaces in between operators.
Code:

`5+5 is the same as 5      +                  5`
Code:

`dMetric= (*piDist1*0.3048)+(*pdDist2*0.254);`
It is kinda confusing, but adding spaces.
Code:

`dMetric= (*piDist1  *  0.3048)+(*pdDist2  *  0.254);`
It is clearly what a pointer points to, time something.
• 11-28-2005
7stud
Quote:

Originally Posted by Enahs
A pointer is not a variable.

A pointer name is a variable just like any other named variable. Simply put, a variable is something that can store different values, i.e. it varies. Here is an example of how a pointer can store different addresses:
Code:

```int num1 = 10; int num2 = 20; int* p = &num1; cout<<*p<<endl; p = &num2; cout<<*p<<endl;```
1) Don't declare global variables. Get rid of these:
Code:

```int iLow=0; int iHigh=0; double dLow=0; double dHigh=0;```
2)Unless typing the word "void" gives you pleasure do this:
Code:

```int main() {```
3)
Code:

```void GetMeasurement() {         int x=0;         double y=0;         int *iDist1=0;         double *dDist2=0;         cout<<"Measurement in feet: "<<endl;         x=GetInt(0, 1000);         iDist1=&x;         cout<<" "<<endl;         cout<<"Measurement in inches:"<<endl;         y=GetDouble(0.0, 11.999);         dDist2=&y; }```
All variables declared in a function(sometimes called "local" variables) are destroyed when the function ends. At the top of the function, you declare the variable:
Code:

`double* dDist2 = 0;`
and at the bottom of the function you assign an address to dDist2:
Code:

`dDist2=&y;`
As soon as execution proceeds to the closing brace of the function on the next line, dDist2 is destroyed and no longer exists. Therefore, assigning a value to dDist2 is pointless. There is still a variable named dDist2 back in main(), but that is a different variable. That leads to a general rule: don't declare a variable with the same name as a variable elsewhere in your program. You declare a variable when you list a type and a name and optionally initialize it. If you know you've declared a variable with a specific name elsewhere in your program, do not declare another variable with the same name.

I would also suggest you abandon your notation:
Code:

`int  *iDist1 = 0;`
Code:

`int*  iDist1 = 0;`
That clearly separates the type of the variable from the variable name. The type is int*(pointer to int) and the variable name is iDist1.

See if that helps you get your program working.
• 11-28-2005
Enahs
Quote:

A pointer name is a variable just like any other named variable.
Yes, but I meant it does not hold the data, but the address to the data.
• 11-29-2005
Atlas24
Quote:

Originally Posted by 7stud
A pointer name is a variable just like any other named variable. Simply put, a variable is something that can store different values, i.e. it varies. Here is an example of how a pointer can store different addresses:
Code:

```int num1 = 10; int num2 = 20; int* p = &num1; cout<<*p<<endl; p = &num2; cout<<*p<<endl;```
1) Don't declare global variables. Get rid of these:
Code:

```int iLow=0; int iHigh=0; double dLow=0; double dHigh=0;```
2)Unless typing the word "void" gives you pleasure do this:
Code:

```int main() {```
3)
Code:

```void GetMeasurement() {         int x=0;         double y=0;         int *iDist1=0;         double *dDist2=0;         cout<<"Measurement in feet: "<<endl;         x=GetInt(0, 1000);         iDist1=&x;         cout<<" "<<endl;         cout<<"Measurement in inches:"<<endl;         y=GetDouble(0.0, 11.999);         dDist2=&y; }```
All variables declared in a function(sometimes called "local" variables) are destroyed when the function ends. At the top of the function, you declare the variable:
Code:

`double* dDist2 = 0;`
and at the bottom of the function you assign an address to dDist2:
Code:

`dDist2=&y;`
As soon as execution proceeds to the closing brace of the function on the next line, dDist2 is destroyed and no longer exists. Therefore, assigning a value to dDist2 is pointless. There is still a variable named dDist2 back in main(), but that is a different variable. That leads to a general rule: don't declare a variable with the same name as a variable elsewhere in your program. You declare a variable when you list a type and a name and optionally initialize it. If you know you've declared a variable with a specific name elsewhere in your program, do not declare another variable with the same name.

I would also suggest you abandon your notation:
Code:

`int  *iDist1 = 0;`
`int*  iDist1 = 0;`