472,365 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,365 software developers and data experts.

Quick Pointer Question


Alright....basically I have this:

Object **node;
..
..
..
node = &(*node)->getNextNode(); // <-- ERROR, where getNextNode
returns an Object *

is there any way to do that last statement without resorting to temp
variables i.e.

Object *node temp;
temp = getNextNode();
node = &temp;

Mar 22 '06 #1
5 1545
Crow wrote:
Alright....basically I have this:

Object **node;
..
..
..
node = &(*node)->getNextNode(); // <-- ERROR, where getNextNode
returns an Object *

is there any way to do that last statement without resorting to temp
variables i.e.

Why bother? a temp often adds clarity.

--
Ian Collins.
Mar 22 '06 #2
Crow wrote:
Alright....basically I have this:

Object **node;
.
.
.
node = &(*node)->getNextNode(); // <-- ERROR, where getNextNode
returns an Object *

You are trying to store the address of a temporary object. Imagine what
happens in the following code:

int f() { return 5 ; }
// ...
int *v = &f() ; // Illegal

What am I trying to assign to v? The only thing that makes sense is
that I am trying to take the address of the temporary created to hold
the return value from f(). What happens when that statement ends and
the temporary is destroyed?
is there any way to do that last statement without resorting to temp
variables i.e.

Object *node temp;
temp = getNextNode();
node = &temp;


Same thing here, except you are copying the temporary created to hold
the return value into your own temporary variable first. What happens
when temp goes out of scope?

Alan
Mar 22 '06 #3
>> node = &(*node)->getNextNode(); // <-- ERROR, where getNextNode returns an Object *
....
....
Object *node temp; Above temp is an Object-type variable *NOT* a pointer type
temp = getNextNode(); I doubt your function really returns a pointer to Object
node = &temp;


Mar 22 '06 #4
Your right Raju, I missed that. It should have been:

Object *temp;
temp = getNextNode();
node = &temp;

Anyway, I get what your said Alan. That makes sense now. I had a
mental lapse dealing with pointers to pointers. Thanks for the tip.

Mar 22 '06 #5

"Crow" <jo******@yahoo.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
Your right Raju, I missed that. It should have been:

Object *temp;
temp = getNextNode();
Did you mean:

temp = (*node)->getNextNode();

?
node = &temp;

Anyway, I get what your said Alan. That makes sense now. I had a
mental lapse dealing with pointers to pointers. Thanks for the tip.


You do realize that now node holds the address of the local variable temp,
right? Is that what you really want, to keep the address of another
(temporary) pointer?

I can't say I've seen much (if any) use of pointer-to-pointers within a
function. Usually, they're just passed as parameters, such as Object**
node, and then *node is operated on directly within the function. That's so
that you can modify the pointer passed to the function. And if that's what
you're really doing here, then when the function exits, node will be
pointing at a local variable that's going out of scope, and you'll be in
trouble.

(And what happened to the old pointer that node previously pointed to? Does
someone else own it? Was it leaked?)

-Howard
Mar 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tom | last post by:
I have made in the past a bunch of VB apps to test hardware. They work and some call functions written in C++ (dll calls). I need to make a quick, dirty and cheap console app to access a PCI...
10
by: Snake | last post by:
Hello guys, I have a quick question about pointers. When I say int *p; *p = 40; cout << *p; why does that produce and error message(something about fault address)? Isnt that I created a...
11
by: JKop | last post by:
Take the following: enum Sense { Vision, Hearing, Touch, Smell };
12
by: Eva | last post by:
Hi, I try to implement quick sort. I sort vectors by their first value. 10 2 3 4 9 3 5 6 10 4 5 6 must be 9 3 5 6 10 2 3 4 10 4 5 6 The prog works great on maybe 500 vectors, but I have an...
27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
16
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last...
5
by: chaoticcranium | last post by:
Right now, I am reading a file line by line using getline() using this code: // Read file line by line, store the first 3 values on each line in a vector and store the vector in a vector of...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.