Advertisement

Easy question about procedures/calls.

Started by November 13, 2000 08:45 PM
2 comments, last by paulcoz 24 years, 1 month ago
I have a working procedure that looks a little like the code below. It is supposed to accept two inputs (size & pt1), and put the result of the procedure in pt2. Everthing works, but I want to do what is desribed below (read on): int size; typedef struct ( double x,y,z; ) pt pt pt1, pt2; UpdatePoint(size,pt1,pt2); // updates a point ---------------------------------------------- void UpdatePoint(int size, pt ptin, pt ptout) ( something is equal to ptin.x * size; ptout.x = something ) ----------------------------------------------- Rather than sending a whole copy of size and pt1 into the procedure I want to just send the addresses of these two points so that I can use their values inside the function without having a copy of the values on the stack, like so: UpdatePoint(&size,&pt1,&pt2); ------------------------------ void UpdatePoint(int *size, pt *ptin, pt *ptout) ( (contents of ptout pointer).x = (contents of ptin pointer).x * (contents of size pointer) ) My function is obviously a little more complex than this, but you get the idea. ptout.x = ptin.x will obviously not work since ptout and ptin are memory addresses not pt structures. pt testpt; testpt = ptin won't work for same reason as above - ptin is a memory address (needs to be contents of ptin). pt testpt; testpt.x = ptin.x ptout.x = testpt.x * size; (DOH!) I really need to know once I have a pointer to a structure in a procedure, how do I access the contents of the memory address pointed to, in the procedure. I need to have access to the input values in the procedure, so I can calculate & put some new values in another memory location. And, also I need another copy of the procedure that puts the new values back in the same memory address (eg. replaces the input values). Can someone clarify how these pointers/procedure calls should be written? Paulcoz. Edited by - paulcoz on 11/13/00 8:53:05 PM
use the -> operator,
ex:
  ptin->x[/source]is equivalent to[source](*ptin).x //dereferencing the x pointer and the accessing a member of the struct[/source]Or you can declare the function as taking "references" to the paramters instead of pointers to them,[source]void UpdatePoint(int& size, pt& ptin, pt& ptout){   ....   ....}  

In this case you access the members of the struct as if you had passed the whole struct on the stack (using the period "." operator), but "behind the scenes" the compiler only passes a pointer and does all the necessary derefencing for you)
Advertisement
Thanks a lot. I don''t suppose you could answer this though:

If you can declare the function as taking "references" to the parameters instead of pointers to them, why would you do it the other way? Is it because you may want access to public/private functions declared in the class, which you can''t get at with a simple address?

Paulcoz.
As I understand the concept (and your post), references are like hidden pointers. You can do *almost* as much with references as with pointers. The problem is, you can''t do everything (at least to my knowledge). If I was doing dynamic memory allocation, I would not want a reference, I would want a pointer, like so:
bool AllocArray(char *Array){    Array = new char[80];    if (Array)        return true;    return false;} 

You can''t make a linked list with references either (unless you are using java ), because you can''t difference a reference.

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming


You are unique. Just like everybody else.

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

This topic is closed to new replies.

Advertisement