How to realize a function call by reference using c?

T

tuchfly

I want to use reference in c just the same as the reference in c++,
but c didn't have reference, how to achieve this goal? Is there a
common method ?
 
K

Keith Thompson

tuchfly said:
I want to use reference in c just the same as the reference in c++,
but c didn't have reference, how to achieve this goal? Is there a
common method ?

Yes, you can simulate pass-by-reference in C by passing the address of
an object.

For example, this C++ code:

void increment(int &arg)
{
arg ++;
}
/* ... */
int x = 42;
increment(x);

can be implemented in C like this:

void increment(int *arg)
{
(*arg) ++;
}
/* ... */
int x = 42;
increment(&x);

Note that you have to explicitly take the address of the object in the
call, and explicitly dereference the passed pointer value in the
function itself.
 
T

tuchfly

Yes, you can simulate pass-by-reference in C by passing the address of
an object.

For example, this C++ code:

    void increment(int &arg)
    {
        arg ++;
    }
    /* ... */
    int x = 42;
    increment(x);

can be implemented in C like this:

    void increment(int *arg)
    {
        (*arg) ++;
    }
    /* ... */
    int x = 42;
    increment(&x);

Note that you have to explicitly take the address of the object in the
call, and explicitly dereference the passed pointer value in the
function itself.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

This is a good method.But what if I want to realize this function,
void f(char &e)
{
e = (char*)malloc(sizeof(char));
}
how to get the same effect?
 
K

Keith Thompson

Please don't quote signatures.
This is a good method.But what if I want to realize this function,
void f(char &e)
{
e = (char*)malloc(sizeof(char));
}
how to get the same effect?

What same effect? That code is incorrect; you're trying to assign a
char* value to a char object. And "(char*)malloc(sizeof(char))" is
better written as "malloc(1)"; the cast is unnecessary and
sizeof(char) is 1 by definition.)

Depending on what you're trying to do, it's often better to return a
result rather than assign it via a pointer parameter.
 
T

tuchfly

"it's often better to return a
result rather than assign it via a pointer parameter."

Thank you very much, I got a solution.
 
B

Beej Jorgensen

Keith Thompson said:
What same effect? That code is incorrect; you're trying to assign a
char* value to a char object. And "(char*)malloc(sizeof(char))" is
better written as "malloc(1)"; the cast is unnecessary and
sizeof(char) is 1 by definition.)

I believe (and I think you might, as well) he's wanting to do something
like this:

void do_alloc(char **p)
{
*p = malloc(1);
}

void caller(void)
{
char *b;

do_alloc(&b);
}

which totally works.
Depending on what you're trying to do, it's often better to return a
result rather than assign it via a pointer parameter.

In this case, agreed.

-Beej
 

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
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top