Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

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


Sign up now

Return reference from assignment

4: Adsense

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.


  • You cannot reply to this topic
9 replies to this topic

#1 Dawoodoz   Members   

459
Like
0Likes
Like

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.

https://en.oxforddictionaries.com/punctuation/punctuation-in-abbreviations


#2 Wooh   Members   

1088
Like
1Likes
Like

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.

#3 Dawoodoz   Members   

459
Like
0Likes
Like

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.

https://en.oxforddictionaries.com/punctuation/punctuation-in-abbreviations


#4 henryksienkiewicz   Members   

178
Like
2Likes
Like

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.

#5 Dawoodoz   Members   

459
Like
0Likes
Like

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.

https://en.oxforddictionaries.com/punctuation/punctuation-in-abbreviations


#6 rip-off   Moderators   

10892
Like
0Likes
Like

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.



#7 Dawoodoz   Members   

459
Like
0Likes
Like

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.

https://en.oxforddictionaries.com/punctuation/punctuation-in-abbreviations


#8 rip-off   Moderators   

10892
Like
0Likes
Like

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.



#9 henryksienkiewicz   Members   

178
Like
1Likes
Like

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.

#10 King Mir   Members   

2490
Like
0Likes
Like

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.