Dear All,
I thought I understood using pointer variables as function parameters.
But I failed to understand why it is needed pass-by-reference of a
pointer variable.
To me, pointer variable is address variable, which holds the memory
address of the object. Using a pointer variable as a function parameter,
the function has the ability to CHANGE the object value pointed by the
pointer. Why needs pass by reference?
For example, I saw some code like the following:
void myfunctionA(int * pA);
void myfunctionB(int *& pA); //what is the advantage?
Thank you very much. 7 2948
"Xiaoshen Li" <xl**@gmu.edu > wrote in message
news:dp******** ***@osf1.gmu.ed u... Dear All,
I thought I understood using pointer variables as function parameters. But I failed to understand why it is needed pass-by-reference of a pointer variable.
Most likely because the function will modify its value. To me, pointer variable is address variable, which holds the memory address of the object.
It can represent the address of an object.
Using a pointer variable as a function parameter, the function has the ability to CHANGE the object value pointed by the pointer.
By dereferencing the pointer, yes it does. Another way would be
to use a reference instead of a pointer.
Why needs pass by reference?
This depends upon what the function does. For example, I saw some code like the following:
void myfunctionA(int * pA);
'myfunction' can modify (and/or inspect) what 'pA' points to.
It cannot modify the caller's argument represented by 'pA'.
void myfunctionA(int *pA)
{
*pA = 0; // OK
pA = 0; // 'pA' is destroyed when function returns,
// caller's original argument is unchanged
} void myfunctionB(int *& pA); //what is the advantage?
Again, 'myfunction' can modify (and/or inspect) what 'pA' points to.
But now, since it has a reference to the caller's argument (the pointer),
it can also modify that caller's argument.
void myfunctionB(int *& pA)
{
*pA = 0; // OK
pA = 0; // modifies caller's argument
}
int main()
{
int i = 1;
int *p = &i;
std::cout << i << '\n'; // prints 1
std::cout << p << '\n'; // prints address of 'i'
myfunctionA(p);
std::cout << i << '\n'; // prints 0
myfunctionB(p);
std::cout << p << '\n'; // prints NULL pointer value'
}
-Mike
Xiaoshen Li wrote: Dear All,
I thought I understood using pointer variables as function parameters. But I failed to understand why it is needed pass-by-reference of a pointer variable.
To me, pointer variable is address variable, which holds the memory address of the object. Using a pointer variable as a function parameter, the function has the ability to CHANGE the object value pointed by the pointer. Why needs pass by reference?
For example, I saw some code like the following:
void myfunctionA(int * pA)
{
static int a[] = { 1, 2 };
pA = a;
} void myfunctionB(int *& pA) //what is the advantage?
{
static int a[] = { 1, 2 };
pA = a;
}
One does what I expect, the other does not.
It does what this dies:
void myfunctionB(int ** pA)
{
static int a[] = { 1, 2 };
*pA = a;
}
Xiaoshen Li wrote: Dear All,
I thought I understood using pointer variables as function parameters. But I failed to understand why it is needed pass-by-reference of a pointer variable.
To me, pointer variable is address variable, which holds the memory address of the object. Using a pointer variable as a function parameter, the function has the ability to CHANGE the object value pointed by the pointer. Why needs pass by reference?
For example, I saw some code like the following:
void myfunctionA(int * pA);
void myfunctionB(int *& pA); //what is the advantage?
You can then modify the pointer itself, apart from the
pointee.
Thank you very much.
you're welcome,
- J.
check this out:
template<typena me T>
void better_delete(T *& ptr) {
delete ptr;
ptr = 0;
}
you delete, and set the pointer to null. this way it's, say, safe to call
better_delete twice on the same pointer :)
"Xiaoshen Li" <xl**@gmu.edu > wrote in message
news:dp******** ***@osf1.gmu.ed u... Dear All,
I thought I understood using pointer variables as function parameters. But I failed to understand why it is needed pass-by-reference of a pointer variable.
To me, pointer variable is address variable, which holds the memory address of the object. Using a pointer variable as a function parameter, the function has the ability to CHANGE the object value pointed by the pointer. Why needs pass by reference?
For example, I saw some code like the following:
void myfunctionA(int * pA);
void myfunctionB(int *& pA); //what is the advantage?
Thank you very much.
Thank you all. Now I think I understand much better now. I still have
one more question:
For a function taking a pointer variable as parameter, actually I saw
two ways to pass the parameter:
void myfunctionA(int * pA)
{
*pA += 1;
}
int main()
{
int *pInt = new int;
*pInt = 77;
myfunctionA(pIn t);
cout << *pInt << endl; //print out 78
//another way of passing parameter
int iNum = 99;
myfunctionA(&iN um);
cout << iNum << endl; //print out 100, correct?
return 0;
}
My question is: for myfunctionB()
void myfunctionB(int *& pA)
{
*pA += 2;
}
Is the following line ok?
myfunctionB(&iN um);
I feel lost with so many &.
Thank you very much.
"Xiaoshen Li" <xl**@gmu.edu > wrote in message
news:dp******** ***@osf1.gmu.ed u... Thank you all. Now I think I understand much better now. I still have one more question:
For a function taking a pointer variable as parameter, actually I saw two ways to pass the parameter:
void myfunctionA(int * pA) { *pA += 1; }
int main() { int *pInt = new int; *pInt = 77; myfunctionA(pIn t); cout << *pInt << endl; //print out 78
//another way of passing parameter
int iNum = 99; myfunctionA(&iN um); cout << iNum << endl; //print out 100, correct?
Correct. return 0; }
My question is: for myfunctionB()
void myfunctionB(int *& pA) { *pA += 2; }
Is the following line ok? myfunctionB(&iN um);
No. Because a temporary object (the pointer returned by the
& operator and passed as the argument) cannot be bound to a
non-const reference.
Write it like this and it will be OK:
void myfunctionB(int * const& pA) /* 'pA' is ref to const pointer to
non-const int */
{
*pA += 2;
} I feel lost with so many &.
You just need to realize that '&' is used for different things
depending upon context (expressed with different syntax).
void f(int& arg); // '&' denotes a reference
int i = 42;
int *p = &i; // '&' denotes 'address of'
int j = i & 2; // '&' denotes bitwise 'and' operation
j = i && 1 // '&&' denotes logical 'and' operation
-Mike This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: M-a-S |
last post by:
Why is there the pass statement? I think, the expression statement would be enough:
class C:
None
while True:
None
|
by: Neal |
last post by:
I need to know how to pass a parameter from PERL to an XSLT when using
that XSLT to transform XML. For instance, I'd like to pass a paramter
that I retrieve from the queryString and pass it into XSLT which will
create my HTML.
Here's what I have for the basic transformation:
#!/usr/local/bin/perl
use XML::XSLT;/
|
by: uli2003wien |
last post by:
Dear group,
PASS (SQL-Server user group) is about to start founding a branch in
Vienna. I went to SQLCON and met some guys from the German PASS group
and decided to become a member of PASS, decided to look for other
individuals to take part in an Austrian branch of PASS based in Vienna,
so whoever is interested may take a look at
http://www.sqlpass.de/Default.aspx?tabid=191 in order to get to contact
me to get things going....
|
by: Alex Nitulescu |
last post by:
Hi.
I have tried to pass two parameters, like this:
Response.Redirect(String.Format("NewPage.aspx?Username={0}, Pass={1}",
txtUserName.Text, txtPass.Text))
But if I pass Username="Alex" and Pass="AAA", I get Params("Username") =
"alex, Pass=AAA" and Params("Pass")="", which is not what I expected.
|
by: Brett |
last post by:
I have several classes that create arrays of data and have certain
properties. Call them A thru D classes, which means there are four. I can
call certain methods in each class and get back an array of data. All four
classes are the same except for the number of elements in their arrays and
the data.
I have a MainClass (the form), which processes all of these arrays. The
MainClass makes use of third party objects to do this. There...
| |
by: Marcelo |
last post by:
Any suggestion?
Thanks
Marcelo
|
by: Ronald S. Cook |
last post by:
I want to something as simple as:
UserControl uctTemp;
But the type will be passed in to the function (which will be an existing
user control like "uctMyUserControl1")
So, how can I pass in the string "uctMyUserControl1" and then somehow dim an
instance of it?
|
by: marshmallowww |
last post by:
I have an Access 2000 mde application which uses ADO and pass through
queries to communicate with SQL Server 7, 2000 or 2005.
Some of my customers, especially those with SQL Server 2005, have had
pass-through queries fail due to intermittent connection failures.
I can easily restablish a connection for ADO.
My problem is with pass-through queries.
|
by: Remote_User |
last post by:
Hi,
Is there any way I can pass a file as a parameter? ( maybe read it
into a object) I am working on C# VS2005.
Thanks.
|
by: mageswar005 |
last post by:
Hello sir,
How can i pass the infinity values from one page to another page with out using post method.
1) I know in Get method some limitations are there.I think Only 1024
characters are pass in GET METHOD.
2) Can any body tell me if i use SESSION METHOD Means ,How many
values can able to pass in SESSION METHOD.
Please some body help me , now i am struggling to pass the bulk(more than...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look !
Part I. Meaning of...
| |
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,...
|
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...
|
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 projectplanning, coding, testing, and deploymentwithout 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...
|
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();...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |