Thread: Calling init function through main in C

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    86

    Calling init function through main in C

    Code:
    #include<stdio.h>
    
     const a * z =Null 
       struct 
     {
     }z;
    
     int init_test (const a *b)
     { 
     z=b;
     }
    
     int main(void){
     {
     init_test(1)
     return0;}
    
    1. I want to call init function through main i dont understand why any one can explain how to od this?
    Last edited by amahmoo; 11-22-2015 at 12:27 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please post your code in [code][/code] bbcode tags without any extra markup. Your code has unnecessary bbcode tags strewn all over, and I'm feeling too lazy to remove them for you.
    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. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    sorry about that i just fixed it

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is init_test supposed to do?
    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

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    86

    init

    Quote Originally Posted by laserlight View Post
    What is init_test supposed to do?
    Its supposed to initalize struct

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Um, I suggest that you go back to your learning material about how to declare struct types and define objects of struct types.

    Here are my specific comments:
    • Code:
      const a * z =Null
      What is a? It is not declared anywhere. Null is wrong. You probably intended NULL. Furthermore, you appear to be trying to declare a global variable named z. Generally, global variables should be avoided.
    • Code:
      struct
      {
      }z;
      Why does the struct type have no members, and why did you declare an object of the struct type with the name z when you apparently wanted to declare a pointer named z just before that?
    • Code:
      init_test(1)
      Why do you pass 1 to init_test?
    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. #7
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Um, I suggest that you go back to your learning material about how to declare struct types and define objects of struct types.

    Here are my specific comments:
    • Code:
      const a * z =Null
      What is a? It is not declared anywhere. Null is wrong. You probably intended NULL. Furthermore, you appear to be trying to declare a global variable named z. Generally, global variables should be avoided.
      I know this your right
    • Code:
      struct
      {
      }z;
      Why does the struct type have no members, and why did you declare an object of the struct type with the name z when you apparently wanted to declare a pointer named z just before that?
      It has no members because im using it as a example
    • Code:
      init_test(1)
      Why do you pass 1 to init_test?
      I dont know what to pass through init_test to call it this is where my question is
      Also the struct is in a head file


    the anwsers are in qoutations

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amahmoo
    I dont know what to pass through init_test to call it this is where my question is
    Well, this is a chicken and egg scenario: what members does the struct have? Presumably you would pass in values to be used to initialise the struct. For example:
    Code:
    #include<stdio.h>
    
    struct X
    {
        int n;
    };
    
    void X_init(struct X *x, int n)
    {
        x->n = n;
    }
    
    int main(void)
    {
        struct X obj;
        X_init(&obj, 123);
        printf("%d\n", obj.n);
        return 0;
    }
    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. #9
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    The struct has function prototypes and its in a headfile

  10. #10
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Thank you so much for ur help I really needed guidance you hae no idea thnk you

  11. #11
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Code:
    #include
    Code:
    <stdio.h>
    struct found in header file
     
       struct 
     {
    (functionprototypes)
     }z;
    
     int init_test (const a *b)
     { 
     z=b;
     }
    const a * z =Null
    
     int main(void){
     {
     init_test(1)
     return0;}

    this is what it has in struct

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Huh? Please be careful when posting code. Your code makes no sense whatsoever. It would help if you stopped replacing code with English sentences.
    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. #13
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Code:
    bool_t func( Di* re )
    {
        bool_t response = true;
     
        if ( re->inst > maxinstant )
        {
            re->errCode = this_is_ error_code;
        }
        else if ( re->inst == 0 )
        {
          response = func1( req );
        }
        else
        {
            Response = func2( req );
        }
     
        return ( response);
    }
    func1 return 1 and func 2 return 0 only
    How would i call in main? func?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amahmoo
    How would i call in main? func?
    When you say "func?", are you asking if this is a way to call the function?
    Code:
    func
    If so, you really need to revise what you should know by now about function calls. You are asking a question to which there can be a multitude of plausible answers. For example:
    Code:
    Di x;
    func(&x);
    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. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Quote Originally Posted by amahmoo View Post
    Code:
    bool_t func( Di* re )
    {
        bool_t response = true;
     
        if ( re->inst > maxinstant )
    ...
        return ( response);
    }
    func1 return 1 and func 2 return 0 only
    How would i call in main? func?
    Goodness, where did this code come from?

    You don't need to identify or call functions with "func" (as it seems perhaps you believe?)

    If you are having trouble understanding function calls, you really need to learn about it, and then try a simple example. Leave out the pointers and all that. If you can't get that to work, post it!

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