Jump to content

  • Log In with Google      Sign In   
  • Create Account


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.

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

#1 Dawoodoz   Members   -  Reputation: 294

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

My open source DirectX 10/11 graphics engine. https://sites.google.com/site/dawoodoz

"My design pattern is the simplest to understand. Everyone else is just too stupid to understand it."


Sponsor:

#2 Wooh   Members   -  Reputation: 591

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   -  Reputation: 294

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.


My open source DirectX 10/11 graphics engine. https://sites.google.com/site/dawoodoz

"My design pattern is the simplest to understand. Everyone else is just too stupid to understand it."


#4 henryksienkiewicz   Members   -  Reputation: 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   -  Reputation: 294

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. :)


My open source DirectX 10/11 graphics engine. https://sites.google.com/site/dawoodoz

"My design pattern is the simplest to understand. Everyone else is just too stupid to understand it."


#6 rip-off   Moderators   -  Reputation: 7963

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   -  Reputation: 294

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.

My open source DirectX 10/11 graphics engine. https://sites.google.com/site/dawoodoz

"My design pattern is the simplest to understand. Everyone else is just too stupid to understand it."


#8 rip-off   Moderators   -  Reputation: 7963

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   -  Reputation: 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   -  Reputation: 1929

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.



PARTNERS