Functions and Pointer
- How to pass pointer to Function?
- Here pointer means passing address to the functions as arguments.
- When we call a function by passing addresses then such call of function is called as call by reference
- Let's make function which will take address as it argument.
Solution:
void reverse_number(int *n) //formal parameters
{ int i,j=0;
while(*n != 0)
{
i = *n%10;
j = j*10+i;
*n = *n/10;
}
*n = j;
}
void reverse_number(int *n) //formal parameters { int i,j=0; while(*n != 0) { i = *n%10; j = j*10+i; *n = *n/10; } *n = j; }
Let's use above function reverse_number():
#include<stdio.h>
void main()
{
int n = 325;
reverse_number(&n); //passing address of variable 'n'.
printf("reverse number is %d",n);
}
#include<stdio.h> void main() { int n = 325; reverse_number(&n); //passing address of variable 'n'. printf("reverse number is %d",n); }
Output:
reverse number is 523
Note:Look clearly in the function definition that we have returned nothing and even then we have reverse the number.
that was because of done changes at the memory location.
see below image of what has happen behind the seen.
Now we will define same function but these time function should take integer number as argument.let's define it.
Solution:
void reverse_number(int n) //formal parameters
{ int i,j=0;
while(n != 0)
{
i = n%10;
j = j*10+i;
n = n/10;
}
*n = j;
}
void reverse_number(int n) //formal parameters { int i,j=0; while(n != 0) { i = n%10; j = j*10+i; n = n/10; } *n = j; }
let's use above function:
#include<stdio.h>
void main()
{
int n = 325;
reverse_number(n); //passing address of variable 'n'.
printf("reverse number is %d",n);
}
#include<stdio.h> void main() { int n = 325; reverse_number(n); //passing address of variable 'n'. printf("reverse number is %d",n); }
Output:
reverse number is 325
As you saw number remain unchange,because we did not made changes in the memory.
Note:we could have reverse the number by returning the value of n while defining the function but we have not done that because we where just understanding the concept of pointers
Defining swap function:
void swap(int *a ,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void swap(int *a ,int *b) { int temp; temp = *a; *a = *b; *b = temp; }
Using above swap() function:
#include<stdio.h>
void main()
{
int i = 10,j= 20;
swap(&i,&j);
printf("after swapping numbers are i = %d and j = %d",i,j);
}
#include<stdio.h> void main() { int i = 10,j= 20; swap(&i,&j); printf("after swapping numbers are i = %d and j = %d",i,j); }
Output:
after swapping numbers are i = 20 and j = 10
Note: there is only one way of swapping number by using user defined function is that argument which are pass to function should be pointers.
because we cannot return multiple value.
see below image of what has happen behind the seen.
Defining function 'max_element':
int * max_element(int *p,int n) //Actual Parameters
{ int i,max=0,*p1;
for(i=0; i<n ; i++)
{ //at every iteration we are comparing the element with
//variable max
//if it is greater than max than we are assigning
//that value to new max value and address of that
//element assigning to pointer variable p1.
if(*(p+i) > max)
{
max = *(p+i);
p1 = (p+i);
}
}
return p1;
}
int * max_element(int *p,int n) //Actual Parameters { int i,max=0,*p1; for(i=0; i<n ; i++) { //at every iteration we are comparing the element with //variable max //if it is greater than max than we are assigning //that value to new max value and address of that //element assigning to pointer variable p1. if(*(p+i) > max) { max = *(p+i); p1 = (p+i); } } return p1; }
Using the above define function:
#include<stdio.h>
void main()
{
int *p,*result;
int i;
p = (int*)malloc(5*sizeof(int));
for(i=0;i<5;i++)
{
printf("enter the element:");
scanf("%d",p+i);
}
result = max_element(p,5); //Actual parameters
printf("Maximum element in array is %d",*result);
}
#include<stdio.h> void main() { int *p,*result; int i; p = (int*)malloc(5*sizeof(int)); for(i=0;i<5;i++) { printf("enter the element:"); scanf("%d",p+i); } result = max_element(p,5); //Actual parameters printf("Maximum element in array is %d",*result); }
Enter the element:10
Enter the element:20
Enter the element:30
Enter the element:40
Enter the element:50
Maximum element in array is 50.
Enter the element:20
Enter the element:30
Enter the element:40
Enter the element:50
Maximum element in array is 50.
Further Topics:
- How to declare Structure Pointer?
- Working with structure Pointer
- How to Create Structure Variable Dynamically ?
- Creating structure variable through malloc() and calloc()
Further Topics:
- How to initialize an structure variable?
- How to Access Structure member?
- What are Nested Structure?
- How to built an Nested Structure?
- How to Access members of Nested Structure?
- How to initialize Nested structure?
- Functions and Structure
- How to pass a structure variable to function?
- How to make function which will take structure variable as arguments?
- How to make function which will return structure variable?
- Array and Structure
- How to make array of structure variable?
- How to initialize array structure variable?
- How to Access members of an array of structure?
- How to take members of array structure as input from the user?
- Pointer and Structure
- How to Point a structure variable?
- How to declare a pointer of structure variable?
- How to Initialize an pointer of structure variable?
- How to access member of structure variable which is point by an pointer?
- What is '->' Operator?
- How to allocate memory for structure variable Dynamically
- How to allocate memory of structure variable using malloc() and calloc() function
- Self Referential Block
- What is Referential Structure?
- How to Create self Referential Structure?
- Applications of Self Referential Structures
- Practice Programs
People Also Searched:
- Functions In C
- Why Functions?
- What are Functions?
- how to write our functions?
- What are programmer define functions?
- How to declare function?
- How to define a function?
- What is function Prototype?
- how to declare a function?
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- Recursion
- Array and Functions
- How to pass an array to a function?
- What is Character array
- Functions for Characters
- Functions from #include<ctype.h> Header File
- what are isdigit(), islower() , isupper(),etc
- what are various string handling functions
- Functions from #include<string.h> Header File
- what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
- strcmp() VS strncmp() VS strcmpi()
- Input And Output Functions
- How to take strings As Input
- What is gets() functions
- What is puts() function
0 Comments
Post a Comment