C Language Tutorials by Ghulam Murtaza Dahar - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Output

Enter 6 numbers:

2

3

4

5

3

4

Sum=21

C Programming Pointers and Functions - Call by Reference

When, argument is passed using pointer, address of the memory location is passed instead of value.

Example of Pointer And Functions

Program to swap two number using call by reference.

/* C Program to swap two numbers using pointers and function. */

#include <stdio.h>

void swap(int *a,int *b);

int main(){

int num1=5,num2=10;

swap(&num1,&num2); /* address of num1 and num2 is passed to swap function */

printf("Number1 = %d\n",num1);

printf("Number2 = %d",num2);

return 0;

}

void swap(int *a,int *b){ /* pointer a and b points to address of num1 and num2

respectively */

int temp;

temp=*a;

*a=*b;

*b=temp;

}