Thread: Calling init function through main in C

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amahmoo
    if the pointer has a value for req then it return 1 if not return 0
    That is not correct. Look carefully at the req->inst. So, when you initialise the Dip object, you need to set its inst member accordingly.

    Quote Originally Posted by amahmoo
    I jusdt dont know how to pass a value for a specifc variable in the struct through the pointer
    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

  2. #32
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    This troubles me:
    Quote Originally Posted by amahmoo View Post
    if the pointer has a value for req
    Do you mean if the pointer has some specific value for req, or do you mean does it have ANY value?

  3. #33
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    it doesnt have any value

  4. #34
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    would i havet o allocate memory for struct in main then set a value for inst and then call it ? can you show me how to do it?

  5. #35
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Look carefully at the req->inst. So, when you initialise the Dip object, you need to set its inst member accordingly.
    how do i iinitialize dip?
    how do i set member accordingly through main

  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amahmoo
    would i havet o allocate memory for struct in main then set a value for inst and then call it ?
    You probably just need to declare an object of type Dip, and then set its member to your desired value.

    Quote Originally Posted by amahmoo
    how do i iinitialize dip?
    how do i set member accordingly through main
    Refer to the earlier posts in this thread and also to your learning material.
    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. #37
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Code:
    dip * req =(dip*)malloc(sizeof(dip);
    Code:
    
    
    how do i declare object how do i set member to desired value?

  8. #38
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, do not use malloc. And um, why does the type name keep changing?
    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. #39
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    i did it on accident
    its same as before
    i just typed fast
    then what am i supposed to use if not malloc

  10. #40
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amahmoo
    then what am i supposed to use if not malloc
    Keep it simple:
    Code:
    Dip 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

  11. #41
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Dip req;
    req-> instance = x;
    func(x);?

  12. #42
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Give it a try. Your compiler will give you some error messages. Read them carefully and try to understand them. This is a good learning experience.
    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. #43
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    prog.c:9:1: error: unknown type name 'bool_t'
    bool_t func( Dip* req )
    ^
    prog.c:9:14: error: unknown type name 'Dip'
    bool_t func( Dip* req )
    ^
    prog.c: In function 'main':
    prog.c:30:5: error: unknown type name 'Dip'
    Dip req;
    ^
    prog.c:31:8: error: invalid type argument of '->' (have 'int')
    req-> instance = x;
    ^
    prog.c:31:22: error: 'x' undeclared (first use in this function)
    req-> instance = x;
    ^
    prog.c:31:22: note: each undeclared identifier is reported only once for each function it appears in
    prog.c:32:5: warning: implicit declaration of function 'func' [-Wimplicit-function-declaration]
    func(x);
    ^
    Last edited by amahmoo; 11-23-2015 at 12:23 PM.

  14. #44
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What do you understand of these error messages?

    And um, you really need to post your 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

  15. #45
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    it doesnt know whot bool_t is
    doesn't know what DIP is
    doesn;t know type of DIP
    prog.c:31:8: error: invalid type argument of '->' (have 'int')
    req-> instance = x;->>>>>>>> i am passing int when there is no int
    x is undeclared
    there is a missing prototype in implixict declaration

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