Thread: Simple Pointer Problem

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    6

    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.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    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?

  3. #3
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    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

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    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

  5. #5
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    In your main you have
    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
    But in your code:
    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.
    Last edited by Enahs; 11-28-2005 at 10:13 PM.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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;
    and adopt this notation instead:
    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.
    Last edited by 7stud; 11-28-2005 at 10:20 PM.

  7. #7
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    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.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    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;
    and adopt this notation instead:
    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.

    Thanks for the help guys. I got her workin. Oh and some of the stuff you guys asked me to do for the syntax, I just put that cuz my programming teacher asks me to do so. Such as the putting void for pleasure thing. (but it does give me some )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 12:42 PM
  2. Problem with function's pointer!
    By Tirania in forum C Programming
    Replies: 5
    Last Post: 11-28-2008, 04:50 AM
  3. printf output makes no sense. Is it a Pointer problem?
    By officedog in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 09:01 AM
  4. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM