Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By stahta01

Please Help

This is a discussion on Please Help within the C Programming forums, part of the General Programming Boards category; Hi Mentors, Please help me with the following code, Code: #include<stdio.h> void main() { int num1, num2; int sum,diff,prod; void ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    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.
    stahta01 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    2,676
    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.
    ankiit likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21