Thread: Calling init function through main in C

  1. #46
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Code:
    #include <stdio.h>
     
    #define maxinstant 1
     
    #define this_error_code 2
    
    
     
     
    bool_t func( Dip* req )
    {
        bool_t response = true;
      
        
        if ( req->inst == 0 )
        {
          response = 0;
        }
        else
        {
            Response = 1;
        }
      
        return ( response);
    }
     
    
    
    int main()
    {
       
        Dip req;
        req-> instance = x;
        func(x);
        return 0;
    }
    this is my code
    Last edited by amahmoo; 11-23-2015 at 12:23 PM.

  2. #47
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amahmoo
    it doesnt know whot bool_t is
    Yes, you need to #include <stdbool.h> and compile with respect to C99 or later.

    Quote Originally Posted by amahmoo
    doesn't know what DIP is
    doesn;t know type of DIP
    The type name is Dip, not DIP. "i just typed fast" is a valid excuse once or twice, but you are carelessly getting it wrong time after time. Likewise, you wrote "inst", then you wrote "instance"... it may seem like a small matter, but if you can be so careless here, you could well be equally careless with the code that you are actually trying to compile, and such carelessness will result in compile or logic errors.

    Quote Originally Posted by amahmoo
    prog.c:31:8: error: invalid type argument of '->' (have 'int')
    req-> instance = x;->>>>>>>> i am passing int when there is no int
    The int thing is a bit of a red herring: the compiler was confused by an earlier error. Rather, the problem is that if you declare req to be a Dip object, then you would access its members as req.instance, not req->instance, because req->instance is equivalent to (*red).instance, but obviously you cannot dereference req because it is not a pointer.

    Quote Originally Posted by amahmoo
    x is undeclared
    Yes; this should be trivial to fix.

    Quote Originally Posted by amahmoo
    there is a missing prototype in implixict declaration
    Do you know what this means?

    EDIT:
    Eh, I just remembered that <stdbool.h> enables bool, not bool_t. I mean, this is related to you pulling code from goodness knows where. You'll need to include the definition of bool_t, whatever it is.
    Last edited by laserlight; 11-23-2015 at 12:33 PM.
    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

  3. #48
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    >> the compiler was confused by an earlier error

    This is an important thing to learn. When you get bombarded with compile errors, you can fix several simple/obvious errors together at once, but the more mysterious (to you) errors should be fixed starting from the first, and one-at-a-time, with recompiling in between. Sometimes one fix eliminates many errors.

  4. #49
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    
    #define maxinstant 1
     
    #define this_error_code 2
    
    
    
    
     
    bool_t func( Dip* req )
    {
        bool_t response = true;
      
        
        if ( req->inst == 0 )
        {
          response = 0
        }
        else
        {
            Response = 1
        }
      
        return ( response);
    }
     
    
    
    int main()
    {
       int x;
        Dip req;
        req.inst = 1;
        func();
        return 0;
    }
    we can ignore implicit declarations warning i understand everything else

  5. #50
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    still dont know what i am doing wrong

  6. #51
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    prog.c:10:1: error: unknown type name 'bool_t'
    bool_t func( Dip* req )
    ^
    prog.c:10:14: error: unknown type name 'Dip'
    bool_t func( Dip* req )
    ^
    prog.c: In function 'main':
    prog.c:31:5: error: unknown type name 'Dip'
    Dip req;
    ^
    prog.c:32:8: error: request for member 'inst' in something not a structure or union
    req.inst = 1;
    ^
    prog.c:33:5: warning: implicit declaration of function 'func' [-Wimplicit-function-declaration]
    func();
    ^
    prog.c:31:9: warning: variable 'req' set but not used [-Wunused-but-set-variable]
    Dip req;
    ^
    prog.c:30:8: warning: unused variable 'x' [-Wunused-variable]
    int x;
    ^

  7. #52
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the definition of bool_t? What is the definition of Dip? These are things that you need to include in the code.
    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

  8. #53
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    how do i define Dip?
    where can i find definition of bool_t?:

  9. #54
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amahmoo
    how do i define Dip?
    where can i find definition of bool_t?:
    Where did you find this code to begin with?
    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

  10. #55
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    class example from my uni

  11. #56
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    i have to define by myself there is no definition

  12. #57
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amahmoo
    class example from my uni
    The same example, or an earlier one, should contain the definition of bool_t and of Dip.

    If not, replace bool_t with int and replace true with 1, and then just come up with your own definition for Dip.
    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

  13. #58
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Code:
    typdef struct dip_gat
    {
    int inst;
    }dip;

  14. #59
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    
    #define maxinstant 1
     
    #define this_error_code 2
    
    
    typdef struct dip_gat
    {
    int inst;
    }Dip;
     
    int func( Dip* req )
    {
        bool_t response = 1;
      
        
        if ( req->inst == 0 )
        {
          response = 0
        }
        else
        {
            Response = 1
        }
      
        return ( response);
    }
     
    
    
    int main()
    {
       
        Dip req;
        req.inst = 1;
        func();
        return 0;
    }

  15. #60
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    prog.c:8:8: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'struct'
    typdef struct dip_gat
    ^
    prog.c:13:11: error: expected declaration specifiers or '...' before 'Dip'
    int func( Dip* req )
    ^
    prog.c: In function 'main':
    prog.c:34:5: warning: statement with no effect [-Wunused-value]
    Dip req;
    ^
    prog.c:34:9: error: expected ';' before 'req'
    Dip req;
    ^
    prog.c:35:5: error: 'req' undeclared (first use in this function)
    req.inst = 1;
    ^
    prog.c:35:5: note: each undeclared identifier is reported only once for each function it appears in
    prog.c:36:5: warning: implicit declaration of function 'func' [-Wimplicit-function-declaration]
    func();
    ^

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