Thread: Help with the below program

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    India
    Posts
    12

    Help with the below program

    The below program is not functioning properly I don't know if I have called the function "Integer" by passing its address properly....Please help....


    insert
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    
    int integer()
    {
      int a;
      scanf("Enter the second number %d",&a);
      return a;
    }
    
    
    int subtract(int c, int (*q)())
    {
      int h;
      h = q; /*integer function called*/
      h = c - h;
      printf("%d",h);
      getch();
    }
    
    
    void main()
    {
      int a;
      scanf("Enter first integer %d", &a);
      int (*p)();
      p = integer; /*to store the address of integer function*/ 
      subtract(a,p); /*Passed the address of function to subtract*/
      getch();
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Despite the comment, you did not actually call the function here:
    Code:
    h = q; /*integer function called*/
    Rather:
    Code:
    h = q(); /*integer function called*/
    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
    May 2012
    Location
    India
    Posts
    12
    k thank you......but if i woul have used

    insert
    Code:
      
    int *h;
    h = q; /*to store the address of integer function*/
    will this work without the braces ??

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dushyant
    will this work without the braces ??
    No, because h is a pointer to int, not a function pointer.
    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
    Jun 2011
    Posts
    88
    Code:
    nt integer()
    {
      int a;
      scanf("Enter the second number %d",&a);
     ...
    your scanf is looking for "Enter the second number [0-9]*" , that does not seem like a reasonable expiation for user input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  2. Get program to copy itself into program files, then start on startup.
    By guitarist809 in forum Windows Programming
    Replies: 6
    Last Post: 03-03-2008, 09:42 AM
  3. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 05:33 PM