EST102 Programming in C
Module V
Pointers and Files-Basics of Pointer: declaring pointers, accessing data though pointers, NULL
pointer,array access using pointers, pass by reference effect. File Operations: open, close, read,
write, append Sequential access and random access to files: In built file handling functions
(rewind() ,fseek(), ftell(),feof(), fread(), fwrite()), simple programs covering pointers and files.
POINTERS
The pointer in C language is a variable which stores the address of another variable.
This variable can be of type int, char, array, function, or any other pointer. The size of the
pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is
2 byte.
Consider the following example to define a pointer which stores the address of an
integer variable.
int n = 10;
int *p = &n; // Variable p of type pointer is pointing to the address of the variable n of type
integer.
Declaration of Pointer Variable
The pointer in C language can be declared using * (asterisk symbol). It is also known
as indirection operator used to dereference a pointer.
datatype *ptrvar;
where ptrvar is any identifier
int *a; //pointer to integer
char *c; //pointer to char
Initialization of Pointer Variable
The process of assigning the address of a variable to a pointer variable is known as
initialization. All uninitialized pointers have some garbage value so it should be initialized.
Example
int a;
int *p; //declaration
p=&a; //initialization
IIPE 1
, EST102 Programming in C
Accessing Data through Pointers
A variable can be accessed through its pointer using unary operator ’*’. Also known
as indirection operator or dereferencing operator.
Consider the following example:
#include<stdio.h>
void main()
{
int n = 10;
int *p;
p = &n;
printf("Address of n=%u\n",&n);
printf("Value of n=%d\n",n);
printf("Address of p=%u\n",&p);
printf("Value of p=%u\n",p);
printf("Value of *p=%d\n",*p); // *p = value of n;
}
OUTPUT
Address of n=6487628
Value of n=10
Address of p=6487616
Value of p=6487628
Value of *p=10
Each variable has two properties: address and value. Address of a variable is the
memory location allocated for that variable. Value of a variable is the value stored in the
memory location allocated to them. The address of a variable can be derived using address
of (&) operator. In the example above, the address of n is 6487628 and value is 10.
Variable Name Address Value
n 6487628 10
When p is assigned with &n, p stores the address of n. Thus p points to n. In order to
retrieve the value of n, we can make use of *p.
Pointer Name Address of p Value of p Asterisk Value(*p)
6487628 10
p 6487616
Address of variable n Value of n
IIPE 2
, EST102 Programming in C
NULL Pointer
A pointer that is not assigned any value but NULL is known as the NULL pointer. If
you don't have any address to be specified in the pointer at the time of declaration, you can
assign NULL value. It will provide a better approach.
int *p=NULL;
OPERATIONS ON POINTERS
Address of a variable can be assigned to a pointer variable.
One pointer variable can be assigned to another pointer variable provided both
points to the items of same datatype.
A NULL value can be assigned to a pointer variable.
An integer quantity can be added to or subtracted from a pointer variable. The
result will be a pointer.
When we increment a pointer, its value is increased by the length of its data type.
One pointer variable can be subtracted from another pointer variable provided both
points to the elements of the same array. The result will be an integer value.
Two pointer variables can be compared provided both points to the items of same
datatype.
A pointer variable can be compared with NULL values.
Write a program to find the sum of two numbers using pointers
#include<stdio.h>
void main()
{
int x,y,sum,*xp,*yp;
printf("Enter value of x:");
scanf("%d",&x);
printf("Enter value of y:");
scanf("%d",&y);
xp=&x;
yp=&y;
sum=*xp+*yp;
printf(" Sum is %d \n",sum);
}
OUTPUT
Enter value of x:10
IIPE 3
Module V
Pointers and Files-Basics of Pointer: declaring pointers, accessing data though pointers, NULL
pointer,array access using pointers, pass by reference effect. File Operations: open, close, read,
write, append Sequential access and random access to files: In built file handling functions
(rewind() ,fseek(), ftell(),feof(), fread(), fwrite()), simple programs covering pointers and files.
POINTERS
The pointer in C language is a variable which stores the address of another variable.
This variable can be of type int, char, array, function, or any other pointer. The size of the
pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is
2 byte.
Consider the following example to define a pointer which stores the address of an
integer variable.
int n = 10;
int *p = &n; // Variable p of type pointer is pointing to the address of the variable n of type
integer.
Declaration of Pointer Variable
The pointer in C language can be declared using * (asterisk symbol). It is also known
as indirection operator used to dereference a pointer.
datatype *ptrvar;
where ptrvar is any identifier
int *a; //pointer to integer
char *c; //pointer to char
Initialization of Pointer Variable
The process of assigning the address of a variable to a pointer variable is known as
initialization. All uninitialized pointers have some garbage value so it should be initialized.
Example
int a;
int *p; //declaration
p=&a; //initialization
IIPE 1
, EST102 Programming in C
Accessing Data through Pointers
A variable can be accessed through its pointer using unary operator ’*’. Also known
as indirection operator or dereferencing operator.
Consider the following example:
#include<stdio.h>
void main()
{
int n = 10;
int *p;
p = &n;
printf("Address of n=%u\n",&n);
printf("Value of n=%d\n",n);
printf("Address of p=%u\n",&p);
printf("Value of p=%u\n",p);
printf("Value of *p=%d\n",*p); // *p = value of n;
}
OUTPUT
Address of n=6487628
Value of n=10
Address of p=6487616
Value of p=6487628
Value of *p=10
Each variable has two properties: address and value. Address of a variable is the
memory location allocated for that variable. Value of a variable is the value stored in the
memory location allocated to them. The address of a variable can be derived using address
of (&) operator. In the example above, the address of n is 6487628 and value is 10.
Variable Name Address Value
n 6487628 10
When p is assigned with &n, p stores the address of n. Thus p points to n. In order to
retrieve the value of n, we can make use of *p.
Pointer Name Address of p Value of p Asterisk Value(*p)
6487628 10
p 6487616
Address of variable n Value of n
IIPE 2
, EST102 Programming in C
NULL Pointer
A pointer that is not assigned any value but NULL is known as the NULL pointer. If
you don't have any address to be specified in the pointer at the time of declaration, you can
assign NULL value. It will provide a better approach.
int *p=NULL;
OPERATIONS ON POINTERS
Address of a variable can be assigned to a pointer variable.
One pointer variable can be assigned to another pointer variable provided both
points to the items of same datatype.
A NULL value can be assigned to a pointer variable.
An integer quantity can be added to or subtracted from a pointer variable. The
result will be a pointer.
When we increment a pointer, its value is increased by the length of its data type.
One pointer variable can be subtracted from another pointer variable provided both
points to the elements of the same array. The result will be an integer value.
Two pointer variables can be compared provided both points to the items of same
datatype.
A pointer variable can be compared with NULL values.
Write a program to find the sum of two numbers using pointers
#include<stdio.h>
void main()
{
int x,y,sum,*xp,*yp;
printf("Enter value of x:");
scanf("%d",&x);
printf("Enter value of y:");
scanf("%d",&y);
xp=&x;
yp=&y;
sum=*xp+*yp;
printf(" Sum is %d \n",sum);
}
OUTPUT
Enter value of x:10
IIPE 3