1

I have begin learning pointers in C.

When I try to return pointer in a function, I'm getting segmentation fault error.

Here is the code :

#include<stdio.h>

int *sum(int *, int *);

int main(void)
{
    int a, b;
    int *ans = NULL;
    printf("Enter number a : ");
    scanf("%d", &a);
    printf("Enter number b : ");
    scanf("%d", &b);

    ans = sum(&a, &b);
    printf("Sum = %d", *ans); 

    return 0;   
}

int *sum(int *p, int *q)
{
    int *result = NULL;
    *result = *p + *q;
    return (result);
}

And the output :

Enter number a : 10
Enter number b : 20
Segmentation fault

Segmentation fault occurs in sum function, when result is declared as pointer. However, I am unable to figure out the reason for the same. Any help regarding this is really appreciable.

1
  • 2
    Dereferencing a null pointer is not legal. Commented May 17, 2016 at 6:34

4 Answers 4

5

You are initing a pointer to NULL and then you are deferencing it: it is Undefined behavior

Change sum function to

int *sum(int *p, int *q)
{
    int *result = malloc(sizeof(int));

    // check if malloc returned a valid pointer before to dereference it
    if (result != NULL)
    {
        *result = *p + *q;
    }

    return (result);
}

and add a free call to free the allocated memory.

    // check if sum function allocate the pointer before to dereference it
    if (ans != NULL)
    {
       printf("Sum = %d", *ans); 
    }

    free(ans);

    return 0;   
}

You could also avoid to use pointer to return value:

#include<stdio.h>

int sum(int *, int *);

int main(void)
{
    int a, b;
    int ans;
    printf("Enter number a : ");
    scanf("%d", &a);
    printf("Enter number b : ");
    scanf("%d", &b);

    ans = sum(&a, &b);
    printf("Sum = %d\n", ans); 

    return 0;   
}

int sum(int *p, int *q)
{
    int result = *p + *q;
    return (result);
}

The sum function could also be like:

int sum (int *p, int *q)
{
    return (*p + *q);
}

EDIT

As @JonathanLeffler wrote in his answer you can do also:

#include<stdio.h>

void sum(int *, int *, int *);

int main(void)
{
    int a, b;
    int ans;
    printf("Enter number a : ");
    scanf("%d", &a);
    printf("Enter number b : ");
    scanf("%d", &b);

    sum(&ans, &a, &b);
    printf("Sum = %d\n", ans);

    return 0;
}

void sum(int *result, int *p, int *q)
{
    *result = *p + *q;
}
Sign up to request clarification or add additional context in comments.

4 Comments

And there's really no benefit to passing the arguments as pointers, so you could use static inline int sum(int p, int q) { return p + q; } if you wanted to, though you'd probably want to place that above where it is used.
@JonathanLeffler Yes, sure, but OP wrote: I have begin learning pointers in C
And one of the lessons to learn about pointers is when to use them and when not to use them.
Thanks! This is really helpful. Using malloc solves this issue. I did came up with alternatives, however I wanted to implement function returning a pointer specifically. Thanks for your help!
4

Allocate memory before trying to store anything, and check the return of malloc()

int *result = NULL;
result = malloc(sizeof(*result));
if(result != NULL)
    *result = *p + *q;
else
    printf("malloc returned error");

Also, check the return of the function in main() and exit accordingly.

int main(void)
{
    .
    .
    .
    ans = sum(&a, &b);
    if(ans == NULL)
        return 0;

    printf("Sum = %d\n", ans);
    free(ans);    //free the memory then
    return 0;
}

3 Comments

Why not: int *result = malloc(sizeof(*result));? And it might be an idea to use if (result != NULL) *result = *p + *q;. There's also no good reason to pass the arguments by pointer rather than just by value.
@JonathanLeffler. Thanks. Added the return value check.
@M.M, All set now. :)
2

A third alternative is to declare ans as a normal int variable in the main function and pass a pointer to it to the sum function, like you do with the other two arguments. This is actually emulating call by reference.

1 Comment

I have taken the liberty to add code to my answer based on your answer.
1

#include<stdio.h>

int *sum(int *, int *);

int main(void)
{
    int a, b;
    printf("Enter number a : ");
    scanf("%d", &a);
    printf("Enter number b : ");
    scanf("%d", &b);

    int* ans = sum(&a, &b);
    printf("Sum = %d", *ans); 

    return 0;   
}

int *sum(int *p, int *q)
{
    int plus = *p + *q;
    int *ans = &plus;
    return ans;
}
Store the Sum in a Different variable plus and create a pointer *ans that points to that variable plus and return the pointer variable ans that holds the address of plus

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.