View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Return reference from assignment

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1Dawoodoz  Members

Posted 19 June 2013 - 06:03 AM

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);
}


"App". is an acronym and must always be followed by a dot.

### #2Wooh  Members

Posted 19 June 2013 - 06:56 AM

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

### #3Dawoodoz  Members

Posted 19 June 2013 - 07:28 AM

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.

"App". is an acronym and must always be followed by a dot.

### #4henryksienkiewicz  Members

Posted 22 June 2013 - 02:04 AM

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.

### #5Dawoodoz  Members

Posted 22 June 2013 - 02:26 AM

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.

"App". is an acronym and must always be followed by a dot.

### #6rip-off  Moderators

Posted 22 June 2013 - 05:47 AM

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.

### #7Dawoodoz  Members

Posted 22 June 2013 - 06:15 AM

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, 22 June 2013 - 06:17 AM.

"App". is an acronym and must always be followed by a dot.

### #8rip-off  Moderators

Posted 22 June 2013 - 06:47 AM

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.

### #9henryksienkiewicz  Members

Posted 22 June 2013 - 09:56 AM

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.

### #10King Mir  Members

Posted 22 June 2013 - 09:14 PM

You might instead do:

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

I think it's clearer.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.