Thread: Please Help

  1. #1
    Registered User ankiit's Avatar
    Join Date
    Jan 2012
    Location
    India
    Posts
    16

    Please Help

    Hi Mentors,

    Please help me with the following code,

    Code:
    #include<stdio.h>
    void main()
    {
    int num1, num2;
    int sum,diff,prod;
    void getNum(int * , int *);
    getNum(&num1,&num2);
    sum = num1+num2;
    printf("Sum is=%d",sum);
    getNum(&num1,&num2);
    diff= num1-num2;
    printf("diff  is=%d",diff);
    getNum(&num1,&num2);
    prod= num1*num2;
    printf("Prod is=%d",prod);
    }
    
    void getNum(int *x, int *y)
    {
     printf("Enter num1=");
     scanf("%d", *x);
     printf("Enter num2=");
     scanf("%d", *y);
    }
    I am not getting the correct output, the number i am entering in the *x and *y is getting negative, i have tried turbo C/C++ and gcc(segmentation fault).

    Please help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Here's what you should be seeing if you're compiling with gcc

    $ gcc -Wall -Wextra foo.c
    foo.c:2: warning: return type of ‘main’ is not ‘int’
    foo.c: In function ‘getNum’:
    foo.c:21: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    foo.c:23: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’


    You already have pointers to ints in getNum, there is no need for any further & or * decoration.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User ankiit's Avatar
    Join Date
    Jan 2012
    Location
    India
    Posts
    16
    Hi,

    I didn't get your point, can you please explain the same briefly.

    Thanks
    Ankit

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    5
    Your problem is withing function:
    Code:
    void getNum(int *x, int *y) { printf("Enter num1="); scanf("%d", *x); it should look like this : scanf("%d", x); printf("Enter num2="); scanf("%d", *y); scanf("%d", *y); }





  5. #5
    Registered User ankiit's Avatar
    Join Date
    Jan 2012
    Location
    India
    Posts
    16
    Quote Originally Posted by Diablo667 View Post
    Your problem is withing function:
    Code:
    void getNum(int *x, int *y) { printf("Enter num1="); scanf("%d", *x); it should look like this : scanf("%d", x); printf("Enter num2="); scanf("%d", *y); scanf("%d", *y); }


    Hi,

    Thanks a lot for the suggestion, though this solved the problem I was having but I am still doubtful why did it worked, as *var refers value at the address stored in <var>, while var stores the address of the another integer variable.

    Please correct me if I am wrong, thanks

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by ankiit View Post
    Hi,

    Thanks a lot for the suggestion, though this solved the problem I was having but I am still doubtful why did it worked, as *var refers value at the address stored in <var>, while var stores the address of the another integer variable.

    Please correct me if I am wrong, thanks
    [/LEFT]
    You are mainly right; but, scanf wants the address to store the result.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User ankiit's Avatar
    Join Date
    Jan 2012
    Location
    India
    Posts
    16
    Quote Originally Posted by stahta01 View Post
    You are mainly right; but, scanf wants the address to store the result.

    Tim S.
    Hi Tim,

    Thanks a lot , got your point.

    regards,
    Ankit

Popular pages Recent additions subscribe to a feed