Thread: Calling init function through main in C

  1. #61
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You made a typo error: typdef should be typedef.

    Note that you forgot to change one more appearance of bool_t to int.

    Finally, you need to pass a pointer to the Dip object in main, and then you probably want to store the return value of the function to print it or something.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #62
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    i am confused with some of these erros

  3. #63
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Fix what I suggested first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #64
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Finally, you need to pass a pointer to the Dip object in main, and then you probably want to store the return value of the function to print it or something.
    how do i do this

  5. #65
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    typedef struct dip_gat
    {
    int inst;
    }Dip;
     
    int func( Dip* req )
    {
        int response = 1;
      
        
        if ( req->inst == 0 )
        {
          response = 0;
        }
        else
        {
            Response = 1;
        }
      
        return ( response);
    }
     
    
    
    int main()
    {
       
        Dip req;
        req.inst = 1;
        func();
        return 0;
    }

  6. #66
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amahmoo
    Finally, you need to pass a pointer to the Dip object in main, and then you probably want to store the return value of the function to print it or something.
    how do i do this
    Recall that &req will result in a pointer to the Dip object named req.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #67
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    you need to pass a pointer to the Dip object in main, <<<<<<<<how do i pass pointer to Dip pbject in main
    and then you probably want to store the return value of the function to print it or something.

  8. #68
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Refer to my post #8.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #69
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Dip *treq =&req<< what does this do
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    
    typedef struct dip_gat
    {
    int inst;
    }Dip;
     
    int func( Dip* req )
    {
        int Response ;
      
        
        if ( req->inst == 0 )
        {
          Response = 0;
        }
        else
        {
            Response = 1;
        }
      
        return ( Response);
    }
     
    
    
    int main()
    {
       
        struct Dip req;
        func(&req,1);
        print("%d\n",req.inst);
        return 0;
    }
    Last edited by amahmoo; 11-23-2015 at 01:08 PM.

  10. #70
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    prog.c: In function 'main':
    prog.c:30:16: error: storage size of 'req' isn't known
    struct Dip req;
    ^
    prog.c:31:5: error: too many arguments to function 'func'
    func(&req,1);
    ^
    prog.c:9:5: note: declared here
    int func( Dip* req )
    ^
    prog.c:32:5: warning: implicit declaration of function 'print' [-Wimplicit-function-declaration]
    print("%d\n",req.inst);
    ^
    prog.c:30:16: warning: unused variable 'req' [-Wunused-variable]
    struct Dip req;
    ^

  11. #71
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read the error messages: too many arguments to function 'func'
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #72
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    
    typedef struct dip_gat
    {
    int inst;
    }Dip;
     
    int func( Dip* req )
    {
        int Response ;
      
        
        if ( req->inst == 0 )
        {
          Response = 0;
        }
        else
        {
            Response = 1;
        }
      
        return ( Response);
    }
     
    
    
    int main()
    {
       
        struct Dip req;
        req.inst =1;
        func(req);
        printf("%d\n",req.inst);
        return 0;
    }
    Last edited by amahmoo; 11-23-2015 at 01:13 PM.

  13. #73
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    prog.c: In function 'main':
    prog.c:30:16: error: storage size of 'req' isn't known
    struct Dip req;
    ^
    prog.c:30:16: warning: unused variable 'req' [-Wunused-variable]

  14. #74
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I give up:
    Code:
    #include <stdio.h>
    
    typedef struct
    {
        int inst;
    } Dip;
    
    int func(Dip *req)
    {
        int response;
    
        if (req->inst == 0)
        {
            response = 0;
        }
        else
        {
            response = 1;
        }
    
        return response;
    }
    
    int main(void)
    {
        int result;
        Dip req;
    
        req.inst = 1;
        result = func(&req);
        printf("inst=%d; result=%d\n", req.inst, result);
    
        req.inst = 0;
        result = func(&req);
        printf("inst=%d; result=%d\n", req.inst, result);
    
        return 0;
    }
    If you are smart, you will do your best to actually understand this, comparing it to your own attempts, instead of just getting spoonfed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #75
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Quote Originally Posted by laserlight View Post
    I give up:
    Code:
    #include <stdio.h>
    
    typedef struct
    {
        int inst;
    } Dip;
    
    int func(Dip *req)
    {
        int response;
    
        if (req->inst == 0)
        {
            response = 0;
        }
        else
        {
            response = 1;
        }
    
        return response;
    }
    
    int main(void)
    {
        int result;
        Dip req;
    
        req.inst = 1;
        result = func(&req);
        printf("inst=%d; result=%d\n", req.inst, result);
    
        req.inst = 0;
        result = func(&req);
        printf("inst=%d; result=%d\n", req.inst, result);
    
        return 0;
    }
    If you are smart, you will do your best to actually understand this, comparing it to your own attempts, instead of just getting spoonfed.
    thank you you taught me alot i understand everything from here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-10-2013, 04:03 PM
  2. Calling this function in my main.
    By filadon in forum C Programming
    Replies: 3
    Last Post: 02-15-2011, 02:58 PM
  3. having some trouble calling a function in the main program
    By CMakesMeSad :( in forum C Programming
    Replies: 19
    Last Post: 06-26-2009, 09:33 PM
  4. calling a function inside main()
    By mero24 in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2005, 01:22 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

Tags for this Thread