0

This has the functionality I want (and it works)

#include <stdio.h>
//includes other libraries needed

int foo();

int main() 
{
    while(true) 
    {

        while(foo()==1) 
        {
            //do something
        }

        //does other unrelated things

    }

}

int foo() 
{
    // Returns if a switch is on or off when the function was called 
    // on return 1;
    // off return 0;
}

However, I would like this to happen:

#include <stdio.h>
//includes other libraries needed

int foo();

int main() 
{
    while(true) 
    {
        //THIS IS THE PROBLEM
        int something = foo();

        while(something==1) 
        {
            //do something
        }

        //does other unrelated things

    }

}

int foo() 
{
    // Returns if a switch is on or off when the function was called 
    // on return 1;
    // off return 0;
}

How can I have the something variable update each time the inner while loop is called? I know it has something to do with & or * for references and pointers but I haven't been able to find an example online about this.

Also I cannot edit anything within the foo() function.

4
  • If such a thing were possible, what are you gaining from it? Commented Oct 7, 2009 at 0:09
  • int (*something)() = foo; while(something()==1) ... ? Commented Oct 7, 2009 at 0:12
  • There is some confusion in the answers regarding what you're asking. Would you like the variable to represent the function itself, or the result of the function? Commented Oct 7, 2009 at 0:22
  • The result of the function at the time the variable is accessed. Commented Oct 7, 2009 at 2:44

4 Answers 4

8

I think this is what you mean: it uses the function pointer to represent the function foo. Later it assigns it to a function bar:

#include <stdio.h>
//includes other libraries needed

int foo();
int bar();

int main() 
{
    while(true) 
    {
        int (*something)() = &foo;

        while(something()==1) 
        {
                something = &bar;
        }

        //does other unrelated things

    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Depending on the details, it may be worth considering a for loop - "for (something = &foo; something () == 1; something = &bar);"
5

Since foo() is returning a value, you need to call the function again to get the latest value. The concise way to do that in C is to do the assignment and check together:

while ((something = foo()) == 1)
{
     // do something
}

Comments

1

To represent a function in a variable use a function pointer

 int (*something)() = &foo;

Comments

0

How about this for your loop:

while(true) 
{
    int something = foo();
    // do whatever here

    if (!something) continue;
    do 
    {
      //do something
    } while(foo()==1)

    //does other unrelated things
}

This is assuming, from your example, that you want to have the something variable to do stuff with before the loop, and don't want a new call to foo() before going into your loop.

If not, then as mentionned in another post you can just do while(something = foo()) == 1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.