• Advertisement
Sign in to follow this  

Return reference from assignment

This topic is 1671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can I assign a value to a variable and return a pointer to the variable that was assigned to within the same expression in C99?

 

This is how it is done in 2 statements but nesting will make it look like pseudo assembler because I need garbage collection for lists:

{
int T;
T = 5 + 5;
Method(&T);
}

Share this post


Link to post
Share on other sites
Advertisement
In C++ you can do
int T;
Method(&(T = 5 + 5));
but I don't think that is possible in C.

Share this post


Link to post
Share on other sites

In C++ you can do

int T;
Method(&(T = 5 + 5));
but I don't think that is possible in C.

 

 

That is good to know if I decide to switch to a C++ backend to support more libraries.

Share this post


Link to post
Share on other sites
You could use , operator, e.g.:
 
#include <stdio.h>

void fun(int *x)
{
        printf("%d\n", *x);
}

int main(void)
{
        int i = 0;
        fun( (i = 5, &i) );
}
Cheers,
hs.

Share this post


Link to post
Share on other sites

You could use , operator, e.g.:
 

#include <stdio.h>

void fun(int *x)
{
        printf("%d\n", *x);
}

int main(void)
{
        int i = 0;
        fun( (i = 5, &i) );
}
Cheers,
hs.

 

 

That works great. :)

Share this post


Link to post
Share on other sites

I don't understand your requirement for mushing all this into a single statement. That code is extremely confusing. There is probably a cleaner way to write this, perhaps something like:

#include <stdio.h>
 
void fun(int *x)
{
    printf("%d\n", *x);
}
 
void fun_with(int *pointer, int value)
{
    *pointer = value;
    fun(pointer);
}
 
int main(void)
{
    int i = 0;
    fun_with(&i, 5);
}

 

Your reference to "garbage collection" is unclear to me too.

Share this post


Link to post
Share on other sites

I don't understand your requirement for mushing all this into a single statement. That code is extremely confusing. There is probably a cleaner way to write this, perhaps something like:

#include <stdio.h>
 
void fun(int *x)
{
    printf("%d\n", *x);
}
 
void fun_with(int *pointer, int value)
{
    *pointer = value;
    fun(pointer);
}
 
int main(void)
{
    int i = 0;
    fun_with(&i, 5);
}

Your reference to "garbage collection" is unclear to me too.

 

I am making a compiler for a high level language that cross compile to C99 and it's own virtual assembler code.

The assignment is just a way to store the value so that the end of the statement can clean it up after concatunating a list and not storing the result.

If I write it separated then I can't use dangling else for "else if" and have to make a nested "if".

Edited by Dawoodoz

Share this post


Link to post
Share on other sites

I'm afraid I don't really understand you, but I guess if the code is being automatically generated then the stylistic issues are perhaps less important.

Share this post


Link to post
Share on other sites
Yes, forgot to add a disclamer - I totally agree that it's unreadable. "," is rarely used for executing statements in sequence.
And using it in a function call makes things even more confusing.

Cheers,
hs.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement