as

Thursday, 13 November 2014

Pointers and Functions


We can pass arguments by reference using pointers in C. We can also return a pointer from the function.

In below function, we have created one function called swap which takes 2 arguments. We have used pointers to pass the arguments by address.

#include<stdio.h>

// function prototype, also called function declaration
void swap(int *p, int *q);

int main()
{

    int a = 10, b = 20;
    //  calling swap function by reference
    printf("Before swapping \n a = %d \n  b = %d",a,b);
    swap(&a, &b); 
    printf("\n After swapping \n a = %d \n b = %d", a, b);
      

}

void swap(int *p, int *q)
{
    int t;
    t = *p;
    *p = *q;
    *q = t;
}



Sponsored Links

Popular Posts

Comments

ShareThis

Google+ Followers

Google+ Badge