473,472 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Question for defining pointer.

Hi All,

I have below function:
//************************************************** *******
struct Tnode {
std::string word;
int count;
Tnode *next;
};

Tnode* new_Tnode(std::string const &word) {
Tnode *node = new Tnode;
node->word = word;
node->count = 1;
node->next = 0;
return node;
}
void add_word(Tnode *&root, std::string const &word) {
Tnode *node = root;
while (1) {
Tnode *&next = node->next; //?????
if (next==0) {
next = new_Tnode(word);
break;
} else
node = next;
}
}
}

.....
//************************************************** ************************
**************

What's the diffrence for "Tnode *&next = node->next;" and "Tnode *next =
node->next;" ?

The same is: why pass Tnode *&root, not Tnode *root.

I tried remove & from above 2 sentences, and the program didn't work.

Thanks.

Franklin
Aug 25 '05 #1
4 1125
Franklin Li wrote:
Hi All,

I have below function:
//************************************************** *******
struct Tnode {
std::string word;
int count;
Tnode *next;
};

Tnode* new_Tnode(std::string const &word) {
Tnode *node = new Tnode;
node->word = word;
node->count = 1;
node->next = 0;
return node;
}
void add_word(Tnode *&root, std::string const &word) {
Tnode *node = root;
while (1) {
Tnode *&next = node->next; //?????
if (next==0) {
next = new_Tnode(word);
break;
} else
node = next;
}
}
}

....
//************************************************** ************************ **************

What's the diffrence for "Tnode *&next = node->next;" and "Tnode
*next = node->next;" ?
The former makes 'next' a reference to (or alias of) the pointer node->next,
so when you change 'next' you are really changing node->next. The latter
places a copy of the address at node->next in 'next', so changing 'next' has
no effect on node->next.
The same is: why pass Tnode *&root, not Tnode *root.
Same reason. The function can change the caller's argument if it's declared
*&, but the function can only change its own copy of the caller's argument
if it's declared *.
I tried remove & from above 2 sentences, and the program didn't work.


No, it wouldn't.

DW
Aug 25 '05 #2
"Franklin Li" <pe*******@hotmai.com> wrote in message
news:de********@netnews.net.lucent.com...
Hi All,

I have below function:
//************************************************** *******
struct Tnode {
std::string word;
int count;
Tnode *next;
};

Tnode* new_Tnode(std::string const &word) {
Tnode *node = new Tnode;
node->word = word;
node->count = 1;
node->next = 0;
return node;
}
void add_word(Tnode *&root, std::string const &word) {
Tnode *node = root;
while (1) {
Tnode *&next = node->next; //?????
if (next==0) {
next = new_Tnode(word);
break;
} else
node = next;
}
}
}

....
//************************************************** ************************ **************

What's the diffrence for "Tnode *&next = node->next;" and "Tnode *next =
node->next;" ?

The same is: why pass Tnode *&root, not Tnode *root.

I tried remove & from above 2 sentences, and the program didn't work.


TNode * next = node->next;

This declares a pointer named "next" whose value is the same as node->next.
If you change the value of next, the value of node->next will remain
unchanged. So, the line

next = new_Tnode(word);

will not affect the value of node->next, just the value of next.

However...

TNode *& next = node->next;

This declares a reference to (an alias for) node->next. Therefore, the line

next = new_Tnode(word);

will change the value of node->next, which, as far as I can tell, is what is
intended.

I don't see why the root pointer is passed to add_word by reference instead
of by value. I don't see how it would make a difference.

--
David Hilsee
Aug 25 '05 #3

Franklin Li wrote:
Hi All,

I have below function:
//************************************************** *******
struct Tnode {
std::string word;
int count;
Tnode *next;
};

Tnode* new_Tnode(std::string const &word) {
Tnode *node = new Tnode;
node->word = word;
node->count = 1;
node->next = 0;
return node;
}
void add_word(Tnode *&root, std::string const &word) {
Tnode *node = root;
while (1) {
Tnode *&next = node->next; //?????
if (next==0) {
next = new_Tnode(word);
A reference is an alias for its initializer. And so, the above
statement is the same as:
node->next = new_Tnode(word);

if next was not a reference, then modifying next would not affect
node->next.
break;
} else
node = next;
}
}
}
root is never modified, and so there's no point in root being a
reference.

....
//************************************************** ************************
**************

What's the diffrence for "Tnode *&next = node->next;" and "Tnode *next =
node->next;" ?

The same is: why pass Tnode *&root, not Tnode *root.

I tried remove & from above 2 sentences, and the program didn't work.
if that explaination wasn't sufficient, please re-read your book.
Thanks.

Franklin


Aug 25 '05 #4
Re-stating the said/obvious:
void add_word(Tnode *&root, std::string const &word) {
Here, "root" is a reference to a pointer. You could just have written
"(Tnode **root," instead of "(Tnode *&root,".

When I write Tnode **root, it means root is a pointer to a pointer that
can point to root ADT.

Since C++ has this poerful concept of references, it is better to use
in this case - i.e prefer a reference to a pointer than a pointer to
pointer when in C++. A reference to a pointer also makes the job easier
as you dont have to dereference the reference (references can not be
de-referenced) and you just need to write name of reference. Like,
"root" instead of "*root". Makes things easier for maintenance. :-)

One last note : you can not have the reverse way : i.e. you can not put
a pointer to a reference. So, this is wrong "root &*arg".

-Viren
Franklin Li wrote: Hi All,

I have below function:
//************************************************** *******
struct Tnode {
std::string word;
int count;
Tnode *next;
};

Tnode* new_Tnode(std::string const &word) {
Tnode *node = new Tnode;
node->word = word;
node->count = 1;
node->next = 0;
return node;
}
void add_word(Tnode *&root, std::string const &word) {
Tnode *node = root;
while (1) {
Tnode *&next = node->next; //?????
if (next==0) {
next = new_Tnode(word);
break;
} else
node = next;
}
}
}

....
//************************************************** ************************
**************

What's the diffrence for "Tnode *&next = node->next;" and "Tnode *next =
node->next;" ?

The same is: why pass Tnode *&root, not Tnode *root.

I tried remove & from above 2 sentences, and the program didn't work.

Thanks.

Franklin


Aug 25 '05 #5

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

Similar topics

51
by: Casper Bang | last post by:
My question is fundamental I beleive but it has been teasing me for a while: I have two classes in my app. The first class is instantiated as a member of my second class. Within this first class,...
24
by: ark | last post by:
Hello group, Could you help me with this: static const int x; ............ something ............. static const int x = 17; It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the...
8
by: Tim Rentsch | last post by:
Here's another question related to 'volatile'. Consider the following: int x; void foo(){ int y; y = (volatile int) x;
28
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: ...
23
by: Kenneth Brody | last post by:
Given the following: char *ptr1, *ptr2; size_t n; ptr2 = ptr1 + n; Assuming ptr1 is a valid pointer, is the following guaranteed to be true? (ptr2 - ptr1) == n
24
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as...
5
by: John | last post by:
I thought I had a fairly good understanding of typedefs until I came across the following passage from C++ Primer, fourth edition by Stanley B. Lippman: "The mistake is in thinking of a typedef...
6
by: mdinino | last post by:
Hi, I am new to C++, and I have a simple question but I can't seem to find a direct answer. If I use new to create something in the scope of a function and then I return the pointer that I...
2
by: Jerry Adair | last post by:
Hi, I have been playing around with the suggestions and code shown in http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.14 regarding memory allocation/deallocation (mem pools). I...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.