Void pointer to pass by reference

J

jim.deveraux

I have this bizarro argument in a function that's been written for me
that I can't figure out. The abstracted function looks like:

void DoSomething(void *&arg)

I think the argument is a void pointer to a pass by reference. But I
can't seem to get it to work. The DoSomething function uses arg to
return a pointer to a dynamically allocated struct.

void DoSomething(void *&arg)
{
myStruct * tmp = new myStruct;

tmp->myParameter = 555;

arg = tmp;
}

Back at the code where I called DoSomething however, I can't seem to
dereference the void pointer correctly and get at the parameters
inside. How do you do it? I tried

void *tmp;
DoSomething((void *) tmp);
(myStruct *) tmp->myParameter;

But no dice. I get error:

error C2227: left of '->myParameter' must point to class/struct/union

Any suggestions? Most of my problem is I don't know how to interpret or
use correctly a void *&arg data type, and can not find resources that
would properly explain it. Also, this implementation is not mine and I
cannot change it to another, simpler format of data type.
 
T

Thomas Lumley

I have this bizarro argument in a function that's been written for me
that I can't figure out. The abstracted function looks like:

void DoSomething(void *&arg)

Looks like C++ to me
I think the argument is a void pointer to a pass by reference. But I
can't seem to get it to work. The DoSomething function uses arg to
return a pointer to a dynamically allocated struct.

void DoSomething(void *&arg)
{
myStruct * tmp = new myStruct;

Definitely looks like C++. You want comp.lang.c++

-thomas
 
R

Robert Gamble

I have this bizarro argument in a function that's been written for me
that I can't figure out. The abstracted function looks like:

void DoSomething(void *&arg)

[snip]

We discuss C over here in comp.lang.c. What you have on your hands is
C++, a completely different language, they discuss that over in
comp.lang.c++.

Robert Gamble
 
G

Guest

/* the following c++ program can
give correct result "555" */
#include<iostream.h>
struct myStruct
{ int myParameter;
};
void DoSomething(void *&arg)
{ myStruct * tmp = new myStruct;
tmp->myParameter = 555;
arg = tmp;
}
void main( )
{ myStruct *p;
DoSomething((void*&)p); //look up here!
cout<<p->myParameter<<endl;
}
 
T

tedu

Back at the code where I called DoSomething however, I can't seem to
dereference the void pointer correctly and get at the parameters
inside. How do you do it? I tried

void *tmp;
DoSomething((void *) tmp);
(myStruct *) tmp->myParameter;

But no dice. I get error:

error C2227: left of '->myParameter' must point to class/struct/union

try:
((myStruct *)tmp)->myParameter;
 
K

Keith Thompson

雨花石c said:
/* the following c++ program can
give correct result "555" */
[snip]

This is comp.lang.c, not comp.lang.c++. Please don't post C++ code here.
 
M

Martin Ambuhl

雨花石c said:
/* the following c++ program can
give correct result "555" */

C++ is a different language, and is off-topic in comp.lang.c. That
having been said, you program is not even a C++ program. Of course you
know that <iostream> is the name of the header and that only fools thing
a C++ main has a void return type. See what else you can fix before you
post your C++ questions to the correct newsgroup. What was your
question, anyway? This can't have been an answer, since you provide no
context.
 
M

Me

/* generic berating you about posting to comp.lang.c instead of
comp.lang.c++ */

void DoSomething(void *&arg)
{
myStruct * tmp = new myStruct;
tmp->myParameter = 555;
arg = tmp;
}

void *tmp;
DoSomething((void *) tmp);

Don't do this cast. You're probably on visual studio which accepts this
but it's supposed to turn this expression into an rvalue and type void
*& can only be bound to an lvalue.
(myStruct *) tmp->myParameter;

This is the same as (myStruct *)(tmp->myParameter). What you want is
((myStruct *)tmp)->myParameter.

???c said:
myStruct *p;
DoSomething((void*&)p); //look up here!

Wrong, this is undefined because of aliasing. This is practically the
same exact example as
http://www.eskimo.com/~scs/c-faq.com/ptrs/genericpp.html
 
K

Keith Thompson

Me said:
/* generic berating you about posting to comp.lang.c instead of
comp.lang.c++ */



Don't do this cast. You're probably on visual studio which accepts this
but it's supposed to turn this expression into an rvalue and type void
*& can only be bound to an lvalue.

There is no type "void *&"; in fact, it's a syntax error.

Unless you're talking about C++, in which case *you're in the wrong
newsgroup*.
 
K

Kenneth Brody

I have this bizarro argument in a function that's been written for me
that I can't figure out. The abstracted function looks like:

void DoSomething(void *&arg)

That's C++. Down the hall, second door on the left.

[...]
void *tmp;
DoSomething((void *) tmp);

No need to case "void *" to "void *".
(myStruct *) tmp->myParameter;

But no dice. I get error:

error C2227: left of '->myParameter' must point to class/struct/union
[...]

Well, this applies to C as well:

((myStruct *) tmp)->myParameter;

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
J

jim.deveraux

Thank you all for replying even though I was quite obviously in the
wrong forum. The problem was solved by using the following syntax:

((myStruct *) tmp)->myParameter

I will post in the right place next time, double promise.
 
V

Vladimir S. Oka

Thank you all for replying even though I was quite obviously in the
wrong forum. The problem was solved by using the following syntax:

((myStruct *) tmp)->myParameter

I will post in the right place next time, double promise.

Most newsgroups will appreciate if you quoted context. The advice on
this page probably applies in most places:

<http://cfaj.freeshell.org/google/>

--
BR, Vladimir

As he came in his chubby choirboy,
Father Burke said, "There's no greater joy!
If no sodomy levens
And possible heavens,
Existence will merely annoy."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,109
Messages
2,570,671
Members
47,263
Latest member
SyreetaGru

Latest Threads

Top