8 Replies - 331 Views - Last Post: 21 September 2011 - 07:49 PM Rate Topic: ***-- 1 Votes

#1 GL666  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 21-September 11

Problems with pointer

Posted 21 September 2011 - 07:17 AM

Hi everyone :D!

Kk, im pretty new in programming and i got some problems with pointer. I know that i can do this little program
without them and its pretty easy, but i want to know how the pointers work between the functions and the main. Ive tried to with the & in front of ((c*e-b*f)/(a*e-b*d)); in the function Cal_xyu but it tells me that it needs 1-value. Then i erase them and it tells me that error C2440: '=' : cannot convert from 'double' to 'double *' . I know that pointers are not ideally placed (otherwise it would work XD). I tried to call the function with a pointer in the function main, but it doesnt work either, the thing compiled but didnt give good answers. I tried so much things, im tried some little tutorials on the matter but i cant understand why it doesnt work. Really my comprehension on the pointers is sooo bad. But w/e, i just want to know how to transfer varaibles from the function in the main with pointers.

thx for the help :D!

#include <iostream>

using namespace std;

void  Cal_xyu (double **x, double **y, double **u);

void ensemble (void);

void ensemble2 (void);

double a,b,c,d,e,f,x,y,u,i,j,k;
	


void main (void)
{
	  ensemble();
void  *Cal_xyu();

 double *i = &x;
 double *j = &y;
 double *k = &u;

	
	while (k == 0)
	{
		cout << "Entrer une autre valeur pour l'une des lettres suivantes a,b,d,e."<< endl;

			ensemble2 ();
	}

	
	
	cout << "Les valeurs de x et y sont : " << endl <<i << " pour x" << endl << j << " pour y"<<endl;
		
		
	system("pause");

}

void ensemble2 (void)
{
	
	cout << "a : ";
	cin >> a;
	cout << "b : ";
	cin >> b;
	cout << "d : ";
	cin >> d;
	cout << "e : ";
	cin >> e;

}

void ensemble (void)
{
	cout << "Entrer des valeurs a, b, c, d, e et f : " << endl;
	cout << "a : ";
	cin >> a;
	cout << "b : ";
	cin >> b;
	cout << "c : ";
	cin >> c;
	cout << "d : ";
	cin >> d;
	cout << "e : ";
	cin >> e;
	cout << "f : ";
	cin >> f;


}



void  Cal_xyu (double*x, double *y, double *u)
{
	 *x = ((c*e-b*f)/(a*e-b*d));
	 *y = ((a*f-c*d)/(a*e-b*d));
	 *u = ((a*e-b*d));
}










Is This A Good Question/Topic? 0
  • +

Replies To: Problems with pointer

#2 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Problems with pointer

Posted 21 September 2011 - 07:46 AM

Quote

Hi everyone :D!

Hi! :)

The main() function must not be return-type void.
It must be of return-type int.

Example . . .
// . . .
int main()
{
    // . . .
}
// . . .


Quote

Then i erase them

What do you mean by "them"? Do you mean the ampersand (&) or the entire line?

Replace the system("pause") lines in your code with:
std::cin.sync();
std::cin.get();


With this, the user must press enter instead of any key. The reason this solution is better than system("pause") is because it is cross-platform, meaning that it can work on Macintosh, Linux, and other platforms than Windows.

And use an indentation style. This makes it easier for other humans to read your code. :P

This post has been edited by hulla: 21 September 2011 - 07:45 AM

Was This Post Helpful? 1
  • +
  • -

#3 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: Problems with pointer

Posted 21 September 2011 - 09:14 AM

Everything hulla said.

#include <iostream>

using namespace std;

void somefunc(int *p);

int main(void) {
	int a = 0;

	somefunc(&a); // Pass the address of 'a'

	cout << "The number you entered is " << a << "!" << endl;

	cin.sync();
	cin.get();

	return 0;
}

void somefunc(int *p) {
	cout << "Please enter a number : ";
	cin >> *p;
}	



Click for Pointers tutorial by dorknexus

Also your function prototype and definition are mismatched, **double != *double.
Was This Post Helpful? 0
  • +
  • -

#4 GL666  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 21-September 11

Re: Problems with pointer

Posted 21 September 2011 - 09:26 AM

kk, i tried new stuff in my program and it compiles but when it does , it tells me 0xC0000005: Access violation writing location 0x00000000. Now im trying to figure out whats wrong with my program, it seems that i cant read or write in a specific location. Do I have to call the function Cal_xyu... in the main to use all the variables inside it or the variables *x,*y or *u can be use from the double *x,*y,*u;? Do I have to use the & somewhere or do I have to leave it like that?

I could change the variables inside the main ex:

double i = *x;
double j = *y;
...

But i dont think it changes anything, my problem still the same.

Anyway, thx for the help!


#include <iostream>

using namespace std;

void  Cal_xyu (double *x,double *y, double *u);

void ensemble (void);

void ensemble2 (void);

double a,b,c,d,e,f;
	
double *x,*y,*u;

void main (void)
{
	  ensemble();
	  Cal_xyu(x,y,u); [i][b]//the crash occurs here...i think[/b][/i]
	
	
	while (*u == 0)
	{
		cout << "Entrer une autre valeur pour l'une des lettres suivantes a,b,d,e."<< endl;

			ensemble2 ();
	}

	
	
	cout << "Les valeurs de x et y sont : " << endl <<*x << " pour x" << endl << *y << " pour y"<<endl;
		
		
	system("pause");

}

void ensemble2 (void)
{
	
	cout << "a : ";
	cin >> a;
	cout << "b : ";
	cin >> b;
	cout << "d : ";
	cin >> d;
	cout << "e : ";
	cin >> e;

}

void ensemble (void)
{
	cout << "Entrer des valeurs a, b, c, d, e et f : " << endl;
	cout << "a : ";
	cin >> a;
	cout << "b : ";
	cin >> b;
	cout << "c : ";
	cin >> c;
	cout << "d : ";
	cin >> d;
	cout << "e : ";
	cin >> e;
	cout << "f : ";
	cin >> f;


}



void  Cal_xyu (double *x, double *y, double *u)
{
	 *x = ((c*e-b*f)/(a*e-b*d));
	 *y = ((a*f-c*d)/(a*e-b*d));
	 *u = ((a*e-b*d));
}



Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1314
  • View blog
  • Posts: 4,514
  • Joined: 19-February 09

Re: Problems with pointer

Posted 21 September 2011 - 09:36 AM

Hi, before you use the pointers they need something to point to.

double *x,*y,*u;




you had this which is better eg i points to the x variable's memory space :

 double *i = &x;
 double *j = &y;
 double *k = &u;


Was This Post Helpful? 0
  • +
  • -

#6 GL666  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 21-September 11

Re: Problems with pointer

Posted 21 September 2011 - 09:36 AM

Hi! :)


Quote

Then i erase them

What do you mean by "them"? Do you mean the ampersand (&) or the entire line?

I erased the ampersand, they werent usefull , i think so :P. But now with my new problem with the location, i think i could use them, but how...I dont know yet, I am working hard to understand all this.

thx for the help!!! :D

And I used those lines in my new version of the program

std::cin.sync();
      std::cin.get();


Was This Post Helpful? 0
  • +
  • -

#7 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: Problems with pointer

Posted 21 September 2011 - 09:42 AM

View PostGL666, on 21 September 2011 - 12:26 PM, said:

double *x,*y,*u;

void main (void)
{
	  ensemble();
	  Cal_xyu(x,y,u); [i][b]//the crash occurs here...i think[/b][/i]



It begins here, but it crashes inside of Cal_xyu. x, y, and u are uninitalized (no storage allocated), inside of Cal_xyu you try to assigne a value you too them, which is causing the crash.

Why did you change your global x, y, and u from double to double *?

Click for Pointers tutorial by dorknexus
Was This Post Helpful? 0
  • +
  • -

#8 GL666  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 21-September 11

Re: Problems with pointer

Posted 21 September 2011 - 11:30 AM

Quote

Why did you change your global x, y, and u from double to double *?


I changed them because if i dont the compiler tells me that i cannot pass from double to double* ...since i changed it, it stopped saying such things. :)

And thx for the info, im trying to work with this atm!
Was This Post Helpful? 0
  • +
  • -

#9 GL666  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 21-September 11

Re: Problems with pointer

Posted 21 September 2011 - 07:49 PM

I found it!!! thx to everyone for the help it si very appreciated!!!! :DDD

#include <iostream>
 
using namespace std;
 
void  Cal_xyu (double*x,double *y, double *u);
 
void ensemble (void);
 
void ensemble2 (void);
 
double a,b,c,d,e,f;
       
double *x = new double;
double *y = new double;
double *u = new double;
 
int main (int)
{
          ensemble();
          Cal_xyu(x,y,u);
 
        double i = *x;
        double j = *y;
        double k = *u;
       
        while (k == 0)
        {
                cout << "Entrer une autre valeur pour l'une des lettres suivantes a,b,d,e."<< endl;
 
                        ensemble2 ();
        }
 
        cout << "Les valeurs de x et y sont : " << endl << i << " pour x" << endl << j << " pour y"<<endl;
       
        std::cin.sync();
        std::cin.get();
 
        delete x;
        delete y;
        delete u;
}
 
void ensemble2 (void)
{
       
        cout << "a : ";
        cin >> a;
        cout << "b : ";
        cin >> b;
        cout << "d : ";
        cin >> d;
        cout << "e : ";
        cin >> e;
 
}
 
void ensemble (void)
{
        cout << "Entrer des valeurs a, b, c, d, e et f : " << endl;
        cout << "a : ";
        cin >> a;
        cout << "b : ";
        cin >> b;
        cout << "c : ";
        cin >> c;
        cout << "d : ";
        cin >> d;
        cout << "e : ";
        cin >> e;
        cout << "f : ";
        cin >> f;
 
 
}
 
 
 
void  Cal_xyu (double *x, double *y, double *u)
{
        double h = ((c*e-b*f)/(a*e-b*d));
        double t = ((a*f-c*d)/(a*e-b*d));
        double r = ((a*e-b*d));
 
        *x = h;
        *y = t;
        *u = r;
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1