Find the answer to your Linux question:
Results 1 to 6 of 6
HI, ************************************************** * #include<iostream.h> void func(void); int main() { if(func) func(); } void func() //empty function { } ************************************************** **** Now in the if condition always comes true as it ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2005
    Location
    India
    Posts
    5

    Using function name in a condition.


    HI,

    ************************************************** *
    #include<iostream.h>

    void func(void);

    int main()
    {
    if(func)
    func();

    }

    void func() //empty function
    {
    }
    ************************************************** ****

    Now in the if condition always comes true as it refers to function address but is it possible to make this condition false. In other words is there any possible way to give func a NULL value.

    Regards
    Nikunj

  2. #2
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,790
    within the function you will be doing something. What you will need to do is either return true or false.

    Since there's nothing in the function at the moment, it's succeeding to run the function, so therefore is true., kinda similar to a while(1).

    you will need to have the function something like:

    Code:
    void func&#40;&#41; //empty function
    &#123;
     do something
     if it succeeded&#123;
        return true
     &#125; else &#123;
        return false
    &#125;
    &#125;
    Not exactly right from syntax point of view, but gived you the idea
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  3. #3
    Linux Guru AlexK's Avatar
    Join Date
    Feb 2005
    Location
    Earth
    Posts
    3,379
    what i don't get is why you would have an if(func) in the 1st place? Just compiling what you've given there returns this:
    Code:
    .
    .
    tr.cpp&#58; In function `int main&#40;&#41;'&#58;
    tr.cpp&#58;7&#58; warning&#58; the address of `void func&#40;&#41;', will always be `true'
    so in conclusion, there seems to be no way to make that if statement become false even if the function returns something. As long as the assembler can find the address of func, it will always be true.
    Life is complex, it has a real part and an imaginary part.

  4. #4
    Just Joined!
    Join Date
    Aug 2005
    Location
    India
    Posts
    5
    Hi,

    What i wanted to know if there is any way to make the func value to be NULL. Moreover i wan't to remove that warning and know if there is anyway to give a function a NULL address without changing its return type.

    Regards
    Nikunj

  5. #5
    Linux Guru AlexK's Avatar
    Join Date
    Feb 2005
    Location
    Earth
    Posts
    3,379
    No, there is no way to make it null with the code you have given, because the assembler will always finds the address of func, thus resulting in a true condition for the if statement.

    If func weren't found, then if statement would become null, but you would get compiler errors asking why func wasn't defined.

    Also there is no way of giving it a null address, because there is no such thing as a null address, the programmer can't directly control the address the function is given on the stack, so there is no way of giving the function a null value unless by some miraculous chance, the address of func was 0x0, which would mean on PC's func would not contain any code.[/code]
    Life is complex, it has a real part and an imaginary part.

  6. #6
    Just Joined!
    Join Date
    Aug 2005
    Location
    India
    Posts
    5
    Thanks for the reply.
    This solves my quest.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •