Click to See Complete Forum and Search --> : What (*&) means?
Chetan
May 9th, 1999, 05:15 PM
Hello gurus,
Can any one tell me what is the meaning of (*&) operator, and why to use it?
Thanks in advance.
Chetan
Practice makes the man perfect.
shellreef
May 9th, 1999, 05:44 PM
*& means to deference the address of a variable. For example, you could do something like:
int n = 10;
printf("%d", (*&n));
Output will be 10.
Sometimes you may need a cast, because dereferencing the address of a variable loses it's type.
Marco
May 9th, 1999, 10:32 PM
what is the difference to :
int n = 10;
printf("%d", n );
??????????????????????????????????????
shellreef
May 9th, 1999, 11:13 PM
By the time printf gets the value, it's the same as if (*&n) where used. & counteracts * acting
like nothing..
For example these will produce same output:
printf("%d", 1); // like printf("%d", n);
printf("%d", 1 + 1 - 1); // like printf("%d", *&n);
See?
It's more steps to get the same result.
sally
May 10th, 1999, 02:37 AM
// A time to use this fancy thingie is in the following:
void CMyObject::DeleteObject( CMyObject*& obj )
{
if (obj != NULL)
{
delete obj;
obj = NULL;
}
}
// after calling this function in the following:
CMyObject* pObj = new CMyObject;
CMyObject::DeleteObject(pObj)
// pObj will be set to NULL....
Sally
May 10th, 1999, 02:37 AM
// A time to use this fancy thingie is in the following:
void CMyObject::DeleteObject( CMyObject*& obj )
{
if (obj != NULL)
{
delete obj;
obj = NULL;
}
}
// after calling this function in the following:
CMyObject* pObj = new CMyObject;
CMyObject::DeleteObject(pObj)
// pObj will be set to NULL....
Hello,
Whats the difference between
void CMyObject::DeleteObject( CMyObject*& obj )
and void CMyObject::DeleteObject( CMyObject* obj )
because i guess both the functions can achieve the same results?
Thanks
Braulio
May 10th, 1999, 09:37 AM
(*&) Is used when you want to pass by reference one pointer, so when you call this the "variable" that you pass by parameter will be modified ( not the content, the adress in wich this pointer point).
If you want I can make you a little example, Bye !
Braulio
PS.: Take a look at CMapStringToPtr, LookUp method, there you must *&
Thanks Braulio,
Sorry for bothering you, but as you said if it is possible for you to give some example about (*&) that will be great help.
Thanks again
Chetan
Braulio
May 10th, 1999, 02:02 PM
Hi Again !
One example ( is no so util, but I think explains how works this well)::
CMyPtr *Ptr = new CMyPtr;
the *Ptr points to for example 0x0034 ( memory)
then we have to functions:
BadDelete(CMyPtr *Pointer)
{
delete Pointer;
Pointer = NULL;
}
GoodDelete(CMyPtr *&Pointer)
{
delete Pointer;
Pointer = NULL;
}
What happens if we made:
CMyPtr *Ptr = new CMyPtr;
BadDelete(Ptr)
Then, the memory will freed, but the "Ptr" pointer will still point to 0x0034
and if we made...
CMyPtr *Ptr = new CMyPtr;
GoodDelete(Ptr)
Then the Ptr after all will be NULL
Why use this, normally, when you call a function that allocates new memory, returns you the pointer as return value of the function, but what about if need to return more pointers, or you want that the return value of your function would be BOOL for example ( take a look at the CMapStringToPtr class, at the Lookup method).
HTH, if you have more questions, please ask, Bye !
Braulio
Michael Decker
May 10th, 1999, 02:29 PM
The difference is that with *& the function DeleteObject() can change the value of the parameter 'obj' and not just what its pointing to. The example given where the person was deleting the pointer and then assigning it to NULL is a good one.
You could also perform the opposite function and allocate memory and assign it to 'obj'.
Before references, you had to pass a pointer to a pointer (which was confusing).
PChetan
May 10th, 1999, 03:30 PM
Thanks Braulio,
Your explaination helped me.
Chetan
Nothing is impossible.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.