Thread: problems with pointers

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    problems with pointers

    I have been working on this for a while now and i am getting an error in line 39 error C2664 'getCoords' : cannot convert parameter 1 from 'double' to 'double*' i am having trouble understanding why this wont work. any input will help.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <cmath>
    using namespace std;
    double getCoords(double *x, double *y, double *a, double *b);
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    double x1, y1, x2, y2;
    double distance, root;
    double *x, *y, *a, *b;
    
    
    
    	cout<<"Enter the x and y values for first coordinate: ";
    	cin>> x1 >> y1;
    
    	cout<<"Enter the x and y values for the second coordinate: ";
    	cin>> x2 >> y2;
    	
    	   x = &x1;
    	   y = &y1;
    	   a = &x2;
    	   b = &y2;
    
    
    
    	 getCoords(*x,*y, *a, *b);
    	{	
    		double g, f, m, n;
    		g = a - x;
    		f = b - y;
    		m = pow(g, 2.0);
    		n = pow(f, 2.0);
    		root = m + n;
    		distance = sqrt(root);
    		return distance;
    	}
    
    	cout<<"The distance between the two coordnates is  "<< distance;
    
    	return 0;

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Nesting functions is illegal in C++, plus you didn't even manage to successfully do that.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code has many errors. Read up on functions for one.
    And you don't need to create local pointers to variables when you pass them to a function!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    15
    Hello, I am afraid, that pointers are not the only problem of your program:
    I will try to list all the errors, syntax is what suffers here, so it shouldn&#180;t be such a problem to fix it:

    1) a superfluous semicolon at the end of "getCoords" line

    2) A function declaration must be written with full type names:
    getCoords(double* x, double* y,double* a, doublle* b)

    3)
    in g= a-x block:
    you are assigning a value of ADRESS pointed by x, you need to deference it like:
    g=*a-*x
    unless you need to get the difference of adresses, which I doubt.

    4) getCoords must be declared outside the main function


    I may still have overlooked something, but I hope this helps.

    edit:

    What I think will help most though, is re-reading the basis of the language, because you seem to holes bigger then that in brain of Mike Tyson.
    Last edited by Garland; 12-17-2007 at 03:53 PM.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    There's also no reason to have the four parameters of getCoords() be pointers. The values of these parameters are used by the function, but none of the values are modified by the function.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not to mention, if they were to be used (which they are not), references could just as easily be used.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    15
    True

    That is the difference between passing by pointer(int* pt) or reference(int& ref) and passing by value (int val) :

    In the first two examples, the address of the variables is passed and they are then modified directly, whereas in the last case, the function makes a copy of the variables and doesn&#180;t really care about the original ones anymore.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. problems passing pointers
    By doormat in forum C Programming
    Replies: 9
    Last Post: 04-11-2004, 04:38 PM
  3. Class Pointers
    By ventolin in forum C++ Programming
    Replies: 8
    Last Post: 04-04-2004, 06:07 PM
  4. Array of Pointers + Deleting An Object = Problems
    By Nereus in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2004, 12:16 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM