Home » C programming language

Pointers as Argument in C programming language.

Pointers as arguments can read/change the values of the variables that are pointed at. In other words we can say that through pointer as an argument we can modify the values of actual arguments of calling function.

For example there is a variable in main function and we want to change the value of the variable through function, the only way to change the value by passing the pointer as an argument, then whatever changes is made in function definition will affect the value of actual argument.

Consider the following code (without passing pointers as arguments)

Function definition

void swapping(int a,int b)
	int temp;

Function calling


In this code we are writing function to swap the values of num1 and num2; but values will not be changed because copies of num1 and num2 are passing to formal parameters a and b.

Consider the following code (passing pointers as arguments)

Function definition

void swapping(int *a,int *b)
	int temp;

Function calling


In this code values of num1 and num2 will be swapped because here we are passing memory address of num1 and num2 that will be copied into pointer variable a and b, so whatever changes will happen with a and b, directly affect to num1 and num2.

Example: C program to swap two numbers using Call by Pointers.

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.