473,387 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1599
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.