Unit 6 C Programming
Unit 6 C Programming
Pointers
________________________________________________________________
Contents
• Concept of Pointer
• Pointer Arithmetic
Address in C
If var is the name of the variable, then &var will give its address.
Declaring a pointer
#include<stdio.h>
int main()
{
int a;
int *ptr;
ptr=&a;
printf("Address of a is
%u",ptr);
}
#include<stdio.h>
int main()
{
float a;
float *ptr;
char b;
char *p;
ptr=&a;
printf("Address of float a is %u\n",ptr);
p=&b;
printf("Address of char a is %u",p);
}
int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of p variable is %x \n",p);
printf("Value of p variable is %d \n",*p);
return 0;
}
Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving
strings, trees, etc. and used with arrays, structures, and functions.
3) It makes you able to access any memory location in the computer's memory.
Usage of pointer
Pointer Arithmetic-
• Increment
• Decrement
• Addition
• Subtraction
Incrementing Pointer in C
Decrementing Pointer in C
C Pointer Addition
We can add a value to the pointer variable. The formula of adding value to
pointer is given below:
C Pointer Subtraction
Like pointer addition, we can subtract a value from the pointer variable.
Subtracting any number from a pointer will give an address.
Program-
#include<stdio.h>
int main()
{
int number=50;
int *p;//pointer to int
p++;
printf("After Increament: Address of p variable is %u \n",p);
p=p+2;
printf("After Addition: Address of p variable is %u \n",p);
return 0;
}
#include<stdio.h>
int main(){
int a,b,c;
int *p,*q;
printf("Enter a and b\n");
scanf("%d%d",&a,&b);
p=&a;
q=&b;
We can use pointer with arrays. Pointers with arrays used to insert
elements into array and also to retrieve array elements. In case of pointer to
arrays pointer holds the base address of array. To move the pointer to next
position the pointer needs to be incremented.
Example-
index 0 1 2 3 4
rollno 12 8 16 7 45
ptr++
#include<stdio.h>
int main()
{
short int a[5]={10,7,6,9,87};
int i;
short int *ptr;
ptr= &a[0];
for(i=0;i<5;i++)
{
printf("%d\n",ptr);
ptr++;
}
}
When address are passed to the function instead of actual values then it is
called as call by reference. While passing parameter using call by address scheme,
we are passing the actual address of the variable to the called function.
Any updates made inside the called function will modify the original
copy since we are directly modifying the content of the exact memory location.
#include <stdio.h>
void increment(int *var)
{
*var = *var+1;
}
int main()
{
int num=20;
increment(&num);
printf("Value of num is: %d", num);
return 0;
}
#include<stdio.h>
struct Player{
int mat;
char name[20];
float avg;
};
void main()
{
struct Player *v; // pointer to structure
struct Player B; // pointer variable
Array of pointer
There could be a situation when we want to maintain an array, which can
store pointers to an int or char or any other data type available. Following is the
declaration of an array of pointers to an integer.
#include<stdio.h>
int main()
{
int i;
for(i=0;i<4;i++)
printf("\nString %s :",arr[i]);
return 0;
}