Solved programming problem

How to return a pointer from a function in C.

Source code

#include <stdio.h>

int *add(int *a, int *b) ///declare a pointer type function.

{

    int ptr;

    ptr=(*a)+(*b);

    return &ptr; ///returning a address

}

int main()

{

    int a,b;

    printf("Please Enter Two integer: ");

    scanf("%d %d",&a,&b);

    int *p=add(&a,&b); ///calling a function with the help of a pointer

    printf("%d",*p); ///printing

    return 0;

}

Output

Please Enter Two integer: 1 2

3

Join the discussion

Add your comment