Immanuel Training Institute proudly presents free software courses
To get certificate Contact: iits.certificate@gmail.com
CHAPTER 1 Introduction and structure of C program
CHAPTER 2 Data types, Constants, Variables and I/O Statements
CHAPTER 3 Operators and Expressions
CHAPTER 6 String Manipulations
CHAPTER 8 Structure, Unions and Enumerated Data types
CHAPTER 9
POINTERS
A Pointer is a derived data type in
C. Pointer contains memory addresses as
their values. Since these memory addresses are the locations in the computer
memory where program instructions and data are stored, pointers can be used to
access and manipulate data stored in the memory. Pointers are more efficient in
handling arrays and data tables.
Declaring the Pointer variable
Syntax
data type *ptr_name;
Example
int *p;
float *p1;
DOUBLE
POINTER
As we know that, a pointer is used to
store the address of a variable in C. Pointer reduces the access time of a
variable. However, In C, we can also define a pointer to store the address of
another pointer. Such pointer is known as a double pointer (pointer to
pointer). The first pointer is used to store the address of a variable whereas
the second pointer is used to store the address of the first pointer.
int **p2;
POINTERS AND
ARRAY
When an array is declared, the
compiler allocates a base address and sufficient amount of storage to contain
all the elements of the array in contiguous memory locations. The base address
is the location of first element (index 0) of the array.
syntax:-
int
x[5]={11,22,33,44,55};
POINTER
CHARACTER STRING
String is treated like character
arrays and therefore they are declared initialized as follows.
char str[5]="good";
The compiler automatically inserts
the null character '\0' at the end of
the string. C supports an alternative method
to creative method to create strings using pointer variables of type char.
char
*str="good";
This creates a string for the
literal and then stores its address in the pointer variable str.
ARRAY OF
POINTER
One important use of pointers in
handling of a table of strings. Consider the following array of strings.
char a[3];
We can make it a pointer to a string
of varying length. For example,
char *a[3];
ex;-char *na[3]={"sir
sai","om sai","jai sai"};
POINTERS AND
STRUCTURES
We know that the name of an array
stands for the address of its zeroth element. The same thing is true of the
names of arrays of structure variables. suppose product is an array variable of
struct type. The name product represents the address of its zeroth element.
syntax:-
struct inventory
{
char na[27];
int no;
float price;
}product[2],*ptr;
POINTER AS
FUNCTION ARGUMENTS
We can pass the address of a
variable as an argument to a function in the normal fashion. We used this method
to function that returns multiple values. When we pass addresses to a function,
the parameters receiving the addresses should be pointers. The process of
calling a function using pointer to pass the addresses of variable is known as
"call by reference" . The function which is called by reference can change
the value of variable used in the call.
Syntax
main()
{
int x;
x=20;
change(&x); /* call by reference or address */
printf("%d\n",x);
}
change(int
*p)
{
*p = *p+10;
}
EXAMPLE
PROGRAMS
1. #include<stdio.h>
#include<conio.h>
void main()
{
float a,b;
clrscr();
printf("Enter the a,b values:");
scanf("%f%f",&a,&b);
printf("\nThe value of a is %f",a);
printf("\nThe value of b is %f",b);
printf("\nThe address of a is %u",&a);
printf("\nThe address of b is %u",&b);
getch();
}
2. #include<stdio.h>
#include<conio.h>
void main()
{
int a,*p;
clrscr();
printf("Enter the a value:");
scanf("%d",&a);
p=&a;
printf("\nThe value of a is %d",a);
printf("\nThe value of a is %d",*p);
printf("\nThe value of a is %d",*(&a));
printf("\n\nThe address of a is %u",p);
printf("\nThe address of a is %u",&a);
printf("\n\nThe value of p is %u",p);
printf("\nThe value of p is %u",*(&p));
printf("\n\nThe address of p is %u",&p);
getch();
}
3. #include<stdio.h>
#include<conio.h>
void main()
{
int a,*p;
clrscr();
printf("Enter the a value:");
scanf("%d",&a);
p=&a;
printf("\nThe value of a is %d",a);
printf("\nThe value of a is %d",*p);
printf("\nThe value of a is %d",*(&a));
printf("\n\nThe address of a is %u",p);
printf("\nThe address of a is %u",&a);
printf("\n\nThe value of p is %u",p);
printf("\nThe value of p is %u",*(&p));
printf("\n\nThe address of p is %u",&p);
printf("\n\nThe increment value of a is %d",++a);
printf("\nThe increment value of a is %d",++(*p));
printf("\nThe decrement value of a is %d",--(*p));
printf("\nThe decrement value of a is %d",--a);
getch();
}
4. #include<stdio.h>
#include<conio.h>
void main()
{
int a,*p,**x;
clrscr();
printf("Enter the a value:");
scanf("%d",&a);
p=&a;
x=&p;
printf("\nThe value of a is %d",a);
printf("\nThe value of a is %d",*p);
printf("\nThe value of a is %d",*(&a));
printf("\nThe value of a is %d",**x);
printf("\nThe value of a is %d",**(&p));
printf("\n\nThe address of a is %u",p);
printf("\nThe address of a is %u",&a);
printf("\nThe address of a is %u",*x);
printf("\n\nThe value of p is %u",p);
printf("\nThe value of p is %u",*(&p));
printf("\nThe address of p is %u",*(&x));
printf("\nThe value of p is %u",*x);
printf("\n\nThe address of p is %u",&p);
printf("\nThe address of p is %u",x);
printf("\n\nThe value of x is %u",x);
printf("\nThe address of x is %u",&x);
printf("\n\nThe increment value of a is %d",++a);
printf("\nThe increment value of a is %d",++(*p));
printf("\nThe decrement value of a is %d",--(*p));
printf("\nThe decrement value of a is %d",--a);
getch();
}
5. #include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,*p;
clrscr();
printf("Enter the size of array:");
scanf("%d",&n);
printf("Enter the values one by one:");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
p=a;
printf("\nThe values are:\n");
for(i=1;i<=n;i++)
printf("%d\t",*(p+i));
getch();
}
6. #include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,j,t,*p;
clrscr();
printf("Enter the size of array:");
scanf("%d",&n);
printf("Enter the values one by one:");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
p=a;
printf("The values before sorting:\n");
for(i=1;i<=n;i++)
printf("%d\t",*(p+i));
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
if(*(p+i)>*(p+j))
{
t=*(p+i);
*(p+i)=*(p+j);
*(p+j)=t;
}
}
}
printf("\n\nThe Ascending order is:\n");
for(i=1;i<=n;i++)
printf("%d\t",*(p+i));
getch();
}
7. #include<stdio.h>
#include<conio.h>
struct account
{
char name[10];
char place[10];
int ac;
};
struct account s1,*ptr;
void main()
{
clrscr();
printf("Enter the name,place & account:");
scanf("%s%s%d",s1.name,s1.place,&s1.ac);
ptr=&s1;
printf("\nThis is what you have entered\n");
printf("\n%s\t%s\t%d",ptr->name,ptr->place,ptr->ac);
getch();
}
8. #include<stdio.h>
#include<conio.h>
struct invent
{
char *name[20];
int no;
float price;
};
void main()
{
struct invent product[10],*ptr;
int n,i;
clrscr();
printf("Enter the size of array:");
scanf("%d",&n);
i=1;
for(ptr=product;ptr<product+n;ptr++,i++)
{
printf("\nEntry %d",i);
printf("\nEnter the product name:");
scanf("%s",ptr->name);
printf("Enter the product number:");
scanf("%d",&ptr->no);
printf("Enter the product price:");
scanf("%f",&ptr->price);
}
printf("\nThe given values are:\n");
ptr=product;
i=1;
while(ptr<product+n)
{
printf("\n\nItem %d",i);
printf("\nItem name : %s",ptr->name);
printf("\nItem code : %d",ptr->no);
printf("\nItem price : %f",ptr->price);
ptr++;
i++;
}
getch();
}
9. #include<stdio.h>
#include<conio.h>
void main()
{
int a,*x,z;
int b,*y;
clrscr();
printf("Enter the a,b values:");
scanf("%d%d",&a,&b);
x=&a;
y=&b;
z=*x+*y;
printf("\nThe value of a is %d",*x);
printf("\nThe value of b is %d",*y);
printf("\nThe value of a&b is %d %d",a,b);
printf("\nThe address of a is %u",x);
printf("\nThe address of b is %u",y);
printf("\nint type ptr incrt is %u",++x);
printf("\nint type ptr incrt is %u",++y);
printf("\n%u",x+3);
printf("\n%d",z);
getch();
}