Sign in to follow this  
Alpha_ProgDes

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 this post


Link to post
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.

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]

Share this post


Link to post
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.

geez..... help please.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
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 integer
int &ra = a; // an integer reference to a
int *pa; // a pointer to an integer
pa = &a; //Take the address of a (&a) and assign it to the pointer
int *& 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 pa
void func2(int** p) {
*p = new int[10];
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this