Pointer
Pointer
ii) We can return multiple values from a function using the pointer.
iii)Pointer reduces the code and improves the performance,it is used to retrieving
strings, trees, etc. and used with arrays, structures, and functions.
iv)In c language, we can dynamically allocate memory where the pointer is used.
What is a pointer?
i) The pointer is a variable which stores the address of another variable.
ii) This pointer variable can be of type int, char, array, function, or any other pointer.
iii)The size of the pointer depends on the architecture.
Integer pointer: -
in 32-bit architecture the size of an integer pointer is 2 bytes. in 64-bit
Character pointer: -
in 32-bit and 64-bit architecture the size of a character pointer is 1 byte. etc.
Float pointer: -
The pointer in c language can be declared using * (asterisk symbol). It is also known as
indirection pointer
Eg-
int *p;
float* p;
char * ch;
POINTER DECLARATION STYLES
2/9/2023
POINTER INITIALIZATION
Syntax :
Data_type *ptr = expression
Where:
data_type = Any Basic Data type
*ptr = pointer variable
expression = another variable or may be constant
2/9/2023
Initialization of pointer variable
int x;
int *p; //Declaration of pointer variable
p=&x; //Initialization of pointer variable
Example: -
#include <stdio.h>
int main() /* output
{
Address of a: 65524
int a = 10;
int *p; Value of a: 10
p = &a; */
2/9/2023
Operation on Pointer
#include<stdio.h>
/* Output
int main() Address of 0th element is 65516
{ Address of 1th element is 65518
int a[5]={10,20,30,40,50}; Address of 2th element is 65520
int *p,i; Address of 3th element is 65522
Address of 4th element is 65524
p=&a[0];
*/
for(i=0;i<5;i++)
{
printf("Address of %dth element is %x\n",i,p);
p++;
}
return 0;
}
Decrement Operation on Pointer
Consider following
int x[5]={1,2,3,4,5};
int *p;
x[0] x[1] x[2] x[3] x[4]
1 2 3 4 5
1000 1002 1004 1006 1008
p = &x[4]=1008;
P--=&x[3]=1006;
P--=&x[2]=1004 ……
Print the address of every element in array
#include<stdio.h>
int main() /* output:---
{ Address of 4th element is 65526
int a[5]={10,20,30,40,50}; Address of 3th element is 65524
Address of 2th element is 65522
int *p,i; Address of 1th element is 65520
p=&a[5]; Address of 0th element is 65518
for(i=4;i>=0;i--) */
{
printf("Address of %dth element is %x\n",i,p);
p--;
}
return 0;
}
Passing array to function using pointer
2/9/2023
1.Call by Value
#include<stdio.h>
void change(int num) {
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(x);//passing value in function
printf("After function call x=%d \n", x);
return 0;
}
Call by Reference
2/9/2023
1.Call by Reference
#include<stdio.h>
void change(int *num)
{
printf("Before adding value inside function num=%d \n",*num);
(*num) += 100;
printf("After adding value inside function num=%d \n", *num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(&x);//passing reference in function
printf("After function call x=%d \n", x);
return 0;
}
Addition exmaple
Swap values using Pointer
Program 01
Write a program to print the elements of a 1D integer array along with memory address of each element using pointer.