Intro of Pointer
Intro of Pointer
#include <stdio.h>
void Hi_function (int times); /* function */
int main()
{
void (*function_ptr)(int); /* function pointer Declaration */
function_ptr = Hi_function; /* pointer assignment */
function_ptr (3); /* function call */
return 0;
}
void Hi_function (int times)
{
int k;
for (k = 0; k < times; k++)
printf("Hi\n");
}
1. We define and declare a standard function which prints a Hi text k times
indicated by the parameter times when the function is called
2. We define a pointer function (with its special declaration) which takes an
integer parameter and doesn't return anything.
3. We initialize our pointer function with the Hi_function which means that the
pointer points to the Hi_function().
4. Rather than the standard function calling by taping the function name with
arguments, we call only the pointer function by passing the number 3 as
arguments, and that's it!
Keep in mind that the function name points to the beginning address of the
executable code like an array name which points to its first element. Therefore,
instructions like function_ptr = &Hi_function and (*funptr)(3) are correct.
NOTE: It is not important to insert the address operator & and the indirection
operator * during the function assignment and function call.
1. Call by Value
In this parameter passing method, values of actual parameters are copied to
function’s formal parameters and the two types of parameters are stored in different
memory locations. So any changes made inside functions are not reflected in actual
parameters of the caller.
/* Program to understand call by value */
#include<stdio.h>
#include<conio.h>
int value (int, int);
int main()
{
int a=5, b=8;
printf("\n Before calling the function a=%d and b=%d",a,b);
value (a,b);
printf("\n After calling the function a=%d and b=%d",a,b);
getch();
}
int value (int a, int b)
{
a++;
b++;
printf("\n Inside function a=%d and b=%d",a,b);
}
2. Call by Reference
Both the actual and formal parameters refer to the same locations, so any changes
made inside the function are actually reflected in actual parameters of the caller.
/* Program to understand call by reference */
#include<stdio.h>
#include<conio.h>
int ref (int *a, int *b);
int main()
{
int a=5, b=8;
printf("\n Before calling the function a=%d and b=%d",a,b);
ref (&a,&b);
printf("\n After calling the function a=%d and b=%d",a,b);
getch();
}
int ref (int *a, int *b)
{
(*a)++;
(*b)++;
printf("\n Inside function a=%d and b=%d",*a,*b);}
// C program to illustrate // C program to illustrate
// Call by Value // Call by Reference
#include<stdio.h> #include<stdio.h>
// Function Prototype // Function Prototype
void swapx(int x, int y); void swapx(int*, int*);
// Main function // Main function
int main() int main()
{ {
int a = 10, b = 20; int a = 10, b = 20;
// Pass by Values // Pass reference
swapx(a, b); swapx(&a, &b);
printf("a=%d b=%d\n", a, b); printf("a=%d b=%d\n", a, b);
return 0; return 0;
} }
// Swap functions that swaps // Function to swap two variables
// two values // by references
void swapx(int x, int y) void swapx(int* x, int* y)
{ {
int t; int t;
t = x; t = *x;
x = y; *x = *y;
y = t; *y = t;
printf("x=%d y=%d\n", x, y); printf("x=%d y=%d\n", *x, *y);
} }
Output: Output:
x=20 y=10 x=20 y=10
a=10 b=20 a=20 b=10
Call by Value vs. Call by Reference
2. The content of one pointer can be assigned to another pointer variable provided
they both are of the same data type.
For example:
p1=p2; /* valid */
f=p1; /* invalid as two pointers are not of same type */
Here p1 is integer type pointer. If p1 points or stores address 65516, then p1+1 means
address pointed by p1+ size in bytes of data type of pointer i.e. 65516+2=65518.
Thus p1 represent address 65518 instead of 65517.
f+1: It specifies an address which is one memory block for float data
(i.e. 4 memory bytes) beyond the address pointed by f.
65506 65510 65514 65518 65518
f f+1 f+2 f+3 f+4
Here f is float type pointer. If pointer f points address 65506, then f+1 points next
block’s address. As float variable takes 4 bytes in size, the block address is 65510.
Thus, f+1= 65506 + one block of memory for float= 65506+4=65510.
4. Two pointer variables can be compared provided the both pointers are defined
as same type.
For examples:
if( p1<p2)
{
/* is valid comparison when p1 and p2 are of same type */
}
6. Two pointer variables cannot be multiplied by each others and added together.
For examples:
p1 + p2; /* invalid */
p1* p2; /* invalid */
Compiled by
Khanal Nishan