C Operator question

Recommended Posts

[quote]
Quote:
 Original post by MindWipe ....and hope your codings goes very well. /MindWipe
You wish... Just tell me why I need to type cast f to (void*&) in this code:
void func(void *&p)
{
....
}

void func2()
{
int *f;

func(f);          <------------
}


[/quote] can anyone explain why: you would put void *&p in the parameter, how i am supposed to read that (or understanding its meaning), and how come the two operators just don't cancel each other out? thanks.

Share on other sites
void func(void *&p) {...}

The two operators don't cancel each other out because the ampersand in the parameter list is a reference operator not an address of operator.

int *f;
func(f);

will probably produce an access violation. Try this:

int f;
func(&f);

instead. Here the ampersand is an address of operator. The call will more likely succeed because now there is memory backing up the pointer.

Edit: I was mistaken here.

[Edited by - LessBread on September 19, 2004 10:51:32 PM]

Share on other sites
note that the posted code is C++ not C.

Share on other sites
actually i don't know whether it's C++ or C, because it isn't my code. it's CPPMaster Poppet. i guess the quote tags didn't get that in there.

so the void (* &p) would be read: p referenced to incoming variable then deference that variable.
am i right? am i clear?

i thought the reference operator and address-of operator were the same thing. or at least did the same thing.

Share on other sites
Quote:
 Original post by LessBreadvoid func(void *&p) {...}The two operators don't cancel each other out because the ampersand in the parameter list is a reference operator not an address of operator.In addition, this:int *f;func(f); will probably produce an access violation. Try this:int f;func(&f); instead. Here the ampersand is an address of operator. The call will more likely succeed because now there is memory backing up the pointer.

No, you wouldn't want to do that, the reason being: the only time you use a reference to a pointer is when you are going to change the actual address the pointer points to. Your code is more likely to cause an access violation than his.

Alpha_ProgDes: It's a reference to a pointer of void type.

Share on other sites
Quote:
 Original post by LessBreadvoid func(void *&p) {...}The two operators don't cancel each other out because the ampersand in the parameter list is a reference operator not an address of operator.In addition, this:int *f;func(f); will probably produce an access violation. Try this:int f;func(&f); instead. Here the ampersand is an address of operator. The call will more likely succeed because now there is memory backing up the pointer.

i'm not understanding something else.

if the function is:
void f (void* &p) {.....}

and you do this:
int num = 5;f(&num);

that would mean that you would be getting the address of num then referencing that address to p, then dereferencing p.
is that right? because even though i could type it out (assuming that i'm right) i still don't understand how that work, much less get that straight in my head.

Share on other sites
it's almost equal in functionality to the C statment f(void **p) with the exception that it can't be null and that you always have one level of indirection already done when using the variable, that is if you would want the addresse of the passed in pointer (p in this case) you would have to write &p (since you already have one level dereferenced by default when using references)

Share on other sites
Quote:
 Original post by Alpha_ProgDesand you do this: int num = 5;f(&num);that would mean that you would be getting the address of num then referencing that address to p, then dereferencing p. is that right? because even though i could type it out (assuming that i'm right) i still don't understand how that work, much less get that straight in my head.

Maybe this will help:
int a; //an integerint &ra = a; // an integer reference to aint *pa; // a pointer to an integerpa = &a; //Take the address of a (&a) and assign it to the pointerint *& rpa = pa; // a reference to the pointer pa which points to a//If i then do:rpa = 0; //pa now points to the address 0, a is not changed.void func(int*& p);func(pa); //pass a reference to the pointer pa which points to a//pa may be changed in the function, ie:void func(int*& p) {  p = new int[10];}//pa would now point to that memory allocated. In C ,and dx functions in general, this is done like so:void func2(int** p);func2(&pa); //pass the address of pavoid func2(int** p) {  *p = new int[10];}

Create an account

Register a new account

• Forum Statistics

• Total Topics
628379
• Total Posts
2982344

• 10
• 9
• 15
• 24
• 11