-5

The program ask the user to enter the two values, after storing the values in the pointers, it print segmentation fault and terminate.why?

// c program to add two numbers using pointers

#include <stdio.h>

int main()
{
    int *ptr1, *ptr2, sum;

    printf ("Enter the two numbers:\n");
    scanf ("%d %d", ptr1, ptr2);

    sum = *ptr1 + *ptr2;
    printf("The result is %d", sum);
    return 0;
rhuann
  • 1
  • 1

1 Answers1

1

It is because you have not allocated memory for ptr1 and ptr2.

#include <stdio.h>

int main()
{
    int *ptr1=0, *ptr2=0, sum=0;

    ptr1= malloc(sizeof(int));
    if(0 == ptr1) //Check if mem allocation is successful.
    {
        printf("Failed to allocate memory for ptr1\n");
        return 0;
    }

    ptr2= malloc(sizeof(int));
    if(0 == ptr2) //Check if mem allocation is successful.
    {
        printf("Failed to allocate memory for ptr2\n");
        free(ptr1); //Free ptr1 memory
        return 0;
    }

    printf ("Enter the two numbers:\n");
    scanf ("%d %d", ptr1, ptr2);

    sum = *ptr1 + *ptr2;
    printf("The result is %d", sum);

    free(ptr2); //Free ptr2 memory
    free(ptr1); //Free ptr1 memory
    return 0;
}
MayurK
  • 1,925
  • 14
  • 27
  • As AriOnhh mentioned in a comment to your question, you don't need pointers for this simple program. – MayurK Oct 25 '16 at 04:50
  • 1
    Well, I suppose it does the job (apart from not checking that the memory allocations worked). But why not `int val1, val2, sum;` and `scanf("%d %d", &val1, &val2);` and `sum = val1 + val2;`? I'm not sure of the benefit of initializing the pointers — they're set by their respective calls to `malloc()`. – Jonathan Leffler Oct 25 '16 at 04:51
  • @JonathanLeffler, Yes we can do that. I was answering why it was giving segmentation fault and how to fix that. I think this program is part of some complex program and rhuann made it simple to ask the question. So I retained his pointer variables. – MayurK Oct 25 '16 at 04:59
  • i am trying to clear my understanding on pointers – rhuann Oct 25 '16 at 05:00
  • Yup; and there's also the comment that says `program to add two numbers using pointers`. Another, other way is `int val1, val2, *ptr1 = &val1, *ptr2 = &val2;`. There are many ways of (politically incorrect treatment of cats removed) dealing with it. – Jonathan Leffler Oct 25 '16 at 05:01
  • @rhuann: the key thing with pointers — all pointers — is to know what they're pointing at. Is the value in the pointer pointing at some memory that it is still valid to access? If not, all sorts of troubles can occur. – Jonathan Leffler Oct 25 '16 at 05:02
  • @JonathanLeffler I added checks for memory allocations. I think it should be fine now. – MayurK Oct 25 '16 at 05:07
  • Error messages can usefully be reported on `stderr` — the standard error channel. But otherwise, yes. I'm not sure I'd be using `malloc()` for someone struggling with pointers, but their time will come (tomorrow or the day after, probably). – Jonathan Leffler Oct 25 '16 at 05:13