Pointer-as-a-function-argument-in c .

In this session i have discussed pointer as a function in C .


We will know the pointer as function argument lets see the below code this is the way by which you can declare the pointer as a function .

    int add(int*a,int*b);//pointer as function argument
    double add(double*a,double*b);//pointer as function argument
    

Example : Write a program that demonstrate the pointer as a function argument .

    #includ<stdio.h> 
    int add (int*a,int*b);
    int main ()
    {
        int a,b,sum;
        printf("Enter the first number");
        scanf("%d",&a);
        printf("Enter the second number");
        scanf("%d",&b);
        sum=add(&a,&b);
        printf("The sum is %d",sum);
    }
    int add(int*a,int*b)
    {
        return *a+*b;
    }
    OUTPUT : Enter the first number 10
             Enter the second number 20
             sum is 30
    



Example : Write a program that swap the two number using the pointer as function argument .

    #includ<stdio.h> 
    int add (int a,int b);
    int add (int*a,int*b);
    int main ()
    {
        int a,b;
        a=10;
        b=20;
        printf("Before swap the value :\n");
        printf("a : %d\n",a);
        printf("b : %d\n",b);
        printf("After swap the value :\n");
        add(&a,&b);
        return 0;
    }
    int add(int*a,int*b)
    {
        int temp;
        temp=*a;
        *a=*b;
        *b=temp;
        printf("a : %d\n",*a);
        printf("b : %d\n",*b);
    }
    OUTPUT : Before swap the value :
             a : 10
             b : 20
             After swap the value :
             a : 20

    

Welcome to AndroidATPoint.

back image                     next image