Thread: Another noob question

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    9

    Another noob question

    hello once agin,

    I am having a real hard time getting the (!negation) to sink home.
    given:

    Code:
    ptr = Strprint;
    if (!(*ptr)(str))
    printf("Done!\n");
    
    return 0;
    
    }

    Here is my problem (and I have read the chapter about it, and looked it up on google) all this true is false and false is true stuff. why are the vaules returned swaped out like that? in the if (!(*ptr)(str)) that means if it is true it moves to the next line? and if it is false, it just returns a vaule?

    Once agin sorry for such basic questions here.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well it's not really clear what is happening from what you have posted. Post the ENTIRE code segment(function).
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    Code:
    #include <stdio.h>
    
    int StrPrint(char *str);
    
    main()
    {
           char str[24] = "Pointing to a function.";
           int (*ptr)(char *str);
    
           ptr = StrPrint;
           if (!(*ptr)(str))
                 printf("Done!\n");
         
           return 0;
    }
    
    int StrPrint(char *str)
    {
            printf("%s\n", str);
            return 0;
    }

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well just take things logically.

    ptr is declared to be a pointer to a function taking as parameter a char* and returning an int.

    Then, ptr is made to point to function StrPrint, which takes as argument a char* and returns an int.

    (*ptr)(str) refers to the return value of the function ptr is pointing at after processing a call taking as argument str. (i.e. the return value of StrPrint(str)).

    Since that function is always returning 0, (*ptr)(str) will be exactly that 0.

    Negating it in the if would yield a non zero (TRUE) value, so the printf("DONE") statement will execute.
    Last edited by claudiu; 10-02-2010 at 01:49 PM. Reason: Worded things a little wrong sorry
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Cleaned it up a little:

    Code:
    #include <stdio.h>
    
    // Change function to accept constant char *s, as you're not changing the      
    // passed-in value in the function. Added a second function for demonstration purposes.
    int StrPrint(const char *str);
    int AsteriskPrint(const char *str);
    
    // Make a nice typedef for our functions, so it's a bit more                    
    // intuitive to read and write                                                  
    typedef int (*MyPrintFunc)(const char *str);
    
    int main(void)
    {
        char str[24] = "Pointing to a function.";
    
        // Create a pointer to one of our functions                                 
        MyPrintFunc printFunc = StrPrint;
    
        // Then use it                                                              
        if (!printFunc(str))
            printf("Done!\n");
    
        // Reassign function pointer to second function                             
        printFunc = AsteriskPrint;
    
        // And use it                                                               
        if (!printFunc(str))
            printf("Done!\n");
    
        return 0;
    }
    
    int StrPrint(const char *str)
    {
        // Return the number of characters printed, instead of just 0.              
        return printf("%s\n", str);
    }
    
    int AsteriskPrint(const char *str)
    {
        return printf("***%s***\n", str);
    }

    An excellent page on function pointers

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    Thank you very much for the ex.'s, and I am sorry I didnt make it very plain in the first post as to my quary.

    I understand function pointers completly, my quary is the !negation.

    I am having trouble with how, if the operand evalutes to nonzero vaule it is logically false, and when the operator yields 1 its logically true, why is this? pretty much what I am saying is I am haveing a problem looking at any:

    Code:
    !(....)(.....)
    where .... is anything.

    but it is wierd I understand:

    Code:
    int i = 7;
    !(i == 7)
    it checks to see if 7 is equal to seven it returns a 0, this i dont understand.
    Last edited by Gikimish; 10-02-2010 at 03:08 PM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Gikimish View Post
    Thank you very much for the ex.'s, and I am sorry I didnt make it very plain in the first post as to my quary.

    I understand function pointers completly, my quary is the !negation.

    I am having trouble with how, if the operand evalutes to nonzero vaule it is logically false, and when the operator yields 1 its logically true, why is this? pretty much what I am saying is I am haveing a problem looking at any:
    In C the if statement only completes on boolean True... that is to say: if (this is true) do this.

    When you use negation in a conditional statement you flip true to false so the statement becomes: if (this is not true) do this.

    Try this for an extremely simple example:
    Code:
    int main(void)
         { int x;
             // this is expected   
            x = 9;
             if ( x < 10)
               puts("X is less than 10")    
    
            // this is negated
            x = 11;
           if (! (x < 10))
              puts("X is not less than 10")
    
    }
    In the first statement the if statement resolves to true -- x is less than 10-- so it prints out.
    In the second statement if resolves to false --x is not less than 10-- it should not print but false negated is true... so it prints.

    Does that help?

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    Very much Thank you very very much!!!

    But it gave me another question,

    Why do this at all, I mean what power does the !negation operator give in the larger picture.
    I mean that would be very simple with:
    Code:
    int main(void)
         { int x;
             // this is expected   
            x = 9;
             if ( x < 10)
               puts("X is less than 10")    
    
            
           else
              puts("X is not less than 10")
    
    }
    Last edited by Gikimish; 10-02-2010 at 03:28 PM.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well, I can see what you are saying. Personally I always try to put things into words when I see a condition.

    So for example when I see if(!(i==5)) I will read "if i is anything but 5".

    Negation is just that, negation. If it bothers you just read the condition without it evaluate it and then flip the result.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Gikimish View Post
    Very much Thank you very very much!!!

    But it gave me another question,

    Why do this at all, I mean what power does the !negation operator give in the larger picture.
    I mean that would be very simple with:
    In the demonstration example it's pretty useless and you are correct, else would take care of the second part. But consider a larger program where you are using settings and flags to instigate certain behaviours... For example you have a setting for "Print out results"... your test could be a simple negation...

    Code:
    int main(void)
       { int PrinterResults = 0;  // no printer
    
          // lots of intervening code
    
          if (! PrinterResults)
            puts("the results are...");
    }
    ... used to display the result on the screen if you don't want to waste paper.

    Other times you may want to test the condition of a status flag... if (!systemerror)... but there's no reason to have an else statement or perhaps the only condition you care about at that moment is the false one.

    As you write bigger and more complex programs you'll find a ton of uses for this little gem.

  11. #11
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    Thank you very very much after a long road I finally understand it and the power it gives.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Gikimish View Post
    Thank you very very much after a long road I finally understand it and the power it gives.
    Glad I could help...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. noob question about RAM
    By crvenkapa in forum Tech Board
    Replies: 2
    Last Post: 03-26-2010, 03:41 PM
  2. Noob Question: Can't compile with "end1"
    By Lillers in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2010, 04:23 AM
  3. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  4. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 01:28 PM
  5. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM