Quote:Original post by MindWipe ....and hope your codings goes very well. /MindWipeYou wish... Just tell me why I need to type cast f to (void*&) in this code: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.void func(void *&p) { .... } void func2() { int *f; func(f); <------------ }
C Operator question
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.
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.
Edit: I was mistaken here.
[Edited by - LessBread on September 19, 2004 10:51:32 PM]
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.
Edit: I was mistaken here.
[Edited by - LessBread on September 19, 2004 10:51:32 PM]
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.
geez..... help please.
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.
geez..... help please.
Quote:Original post by LessBread
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.
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.
Quote:Original post by LessBread
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.
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.
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)
Quote:Original post by Alpha_ProgDes
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.
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];}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement