473,883 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quick Pointer Question


Alright....basi cally 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 1618
Crow wrote:
Alright....basi cally 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....basi cally 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.goo glegroups.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
2250
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 device. There is a program available to enumerate PCI devices and give the address of the registers assigned during the POST of the PC. That info can be a manual input at program
10
1496
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 pointer and I made it point to the value of 40 and then display it?! Thanx alot,any help is really appreciated
11
1428
by: JKop | last post by:
Take the following: enum Sense { Vision, Hearing, Touch, Smell };
12
2931
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 "Aborted(core
27
3418
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 as ff: typedef struct { float *data ; int count ;
16
6095
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 element in the container? Or is there a cleaner way of getting an iterator to the last element?
5
1474
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 vectors while(getline(peakReader, line)) { istringstream lineStr(line); vector<intpeak(3); if (!(lineStr >peak >peak >peak)) { cerr << "Error reading peak file; will continue
0
9791
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10412
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9571
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7970
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7123
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5797
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4610
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4215
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3231
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.