Reputation: 87
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.
Upvotes: 1
Views: 2567
Reputation: 119
#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 = +
return ans;
}
plus
and create a pointer *ans
that points to that variable plus
and return the pointer variable ans
that holds the address of plus
Upvotes: 1
Reputation: 12272
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;
}
Upvotes: 4
Reputation: 16243
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;
}
Upvotes: 5
Reputation: 409432
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.
Upvotes: 2