0% found this document useful (0 votes)
12 views17 pages

Unit5 C Program

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 17

UNIT – V Programming in C

Pointers:

Pointer is a derived type in C. Pointer is a variable it has the ability to store the address of another
variable of that type. Size of the pointer variable is two bytes. Pointer variable declaration in very similar
to normal variable declaration, but asteric (*) is precede in variable name.

Syntax:
datatype *var1,*var2,….,*varn;

Ex:
int *ip;
float *fp;
Accessing address of a variable:

We can get the address of a variable using the operator &. The operator & immediately preceding a
variable return the address of the variable associated with it.

Ex:
int quantity,*p;
p=&quantity;
in the above example address of the variable quantity will be stored in the pointer variable P.

Example:
#include<stdio.h>
#include<conio.h>

void main()
{

int x,*p;
clrscr();
x=25;
p=&x;
printf("\n Value of x is : %d",x);
printf("\n Address of x is : %u",p);

MS Page 1
UNIT – V Programming in C

printf("\n Value of x is : %d",x);


getch();
}

Output:
Value of x is : 25
Address of x is : 65524
Value of x is : 25

Advantages of pointers:
 Pointers are more efficient in handling arrays.
 With the help of pointer a function can return multiple values.

 With the help of pointer C can support dynamic memory allocation.

Chain of pointers:
It is possible to make a pointer to point to another pointer, thus creating a chain of pointers. Here,
the pointer variable p2 contains the address of the pointer variable p1.

A variable that is a pointer to a pointer must be declared using additional indirection operator in
front of the name.

Ex:

int **p2; /* pointer to a pointer */

Example:
#include<stdio.h>

#include<conio.h>

void main()
{
int x=15,*p1,**p2;

MS Page 2
UNIT – V Programming in C

clrscr();
p1=&x; /* address of x */
p2=&p1; /* address of pointer p1 */

printf("\n Value of x is : %d",x);


printf("\n Address of x is : %u",p1);
printf("\n Value of x is : %d",*p1);
printf("\n Address of p1 is : %u",p2);
printf("\n Address of x is : %u",*p2);
printf("\n Value of x is : %d",**p2);
getch();

Output:
Value of x is : 15
Address of x is : 65524
Value of x is : 15
Address of p1 is : 65522

Address of x is : 65524
Value of x is : 15

Pointer Expression:
Like other variables, pointer variables can be used in expression. This kind of expression is called
pointer expression.

y=*p1 * *p2; /* p1 and p2 are pointer variables */

Pointers and arrays:


When an array is declared, the compiler allocates a base address and sufficient amount of storage to
contain all the element of the array in contiguous memory locations. The base address is the location of the
first element of the array.

int x[5]={2,7,9,3,8};

MS Page 3
UNIT – V Programming in C

Suppose the base address of x is 100 and assuming that each integer requires two bytes. The five elements
will be stored as follows.

The name X can be defined as a constant pointer pointing to the first element x[0], and therefore the
value of x is 100. It can be written as follows,

X=&X[0]=100

If we declare P as an integer pointer, then we can make the pointer P to point the array X by the
following assignment.

P= X;

Example:

#include<stdio.h>
#include<conio.h>

void main()
{
int x[5]={2,7,9,3,8};

int *p,i;
clrscr();

p=x; /* address of x is assigned to p */


printf("\n Your values are :\n");
printf("\n Index Value Address\n");

for(i=0;i<5;i++)
printf("\n x[%d] %d %u",i, *(p+i),(p+i));

MS Page 4
UNIT – V Programming in C

getch();
}

Output:
Your values are :

Index Value Address


x[0] 2 65516
x[1] 7 65518
x[2] 9 65520
x[3] 3 65522
x[4] 8 65524

Arrays of pointers:
We can also have array of pointers. Pointers are very helpful in handling character array with rows
of variable length.

Example:
#include<stdio.h>
#include<conio.h>

void main()
{
char *location[20]={"Chennai", "Delhi","Pune","Mumbai"}; /* Array of pointers */
int i;
clrscr();

printf("Your cities are :\n");


for(i=0;i<4;i++)
printf("\n%s",*(location+i));
getch();
}

MS Page 5
UNIT – V Programming in C

Output:
Your cities are :
Chennai

Delhi
Pune
Mumbai

Pointers as function arguments:


In C language it is possible to pass the address of a variable to a function as a parameter. This process is
called call by reference.

Write a C program to swap two values using call by reference method.


#include<stdio.h>

#include<conio.h>

void swap(int *,int *);

void main()
{

int a,b;
clrscr();
printf("Enter your a and b values :\n");
scanf("%d%d",&a,&b);
printf("\nBefore swapping:\n");
printf("a = %d b = %d",a,b);

swap(&a,&b); /* call by reference or address */


printf("\nAfter Swapping:\n");
printf("a = %d b = %d",a,b);
getch();
}

MS Page 6
UNIT – V Programming in C

void swap(int *p,int *q)


{

int k;
k=*p;
*p=*q;
*q=k;
}

Output:
Enter your a and b values :

20
40
Before swapping:
a = 20 b = 40
After Swapping:
a = 40 b = 20

Differences between call by value and call by reference methods

Call by value Call by Reference

Sending the value of the argument Sending the address of the argument

It is the duplicate of the actual argument It is the synonym of the actual argument

Changes of formal arguments will not affect the Changes of formal arguments will affect the actual
actual arguments. arguments.

Pointers with structure:


In C we can declare a pointer variable using structure name. This process is called pointers with
structure. We can use arrow (->) operator to access the member of the structure.

Write a C program to get item code, price and name and increase its price by Rs. 10

MS Page 7
UNIT – V Programming in C

#include<stdio.h>
#include<conio.h>

struct item
{
int icode;
float price;
char name[25];
};
void inc(struct item *);

void main()
{
struct item x;
clrscr();
printf("Enter your icode, price and name :\n");
scanf("%d%f%s",&x.icode,&x.price,x.name);

printf("\nBefore price increment :");


printf("\nItem Code : %d",x.icode);
printf("\nItem Name : %s",x.name);
printf("\nItem Price : %f",x.price);
inc(&x);
printf("\n\nAfter price increment :");

printf("\nItem Code : %d",x.icode);


printf("\nItem Name : %s",x.name);
printf("\nItem Price : %f",x.price);
getch();
}

MS Page 8
UNIT – V Programming in C

void inc(struct item *p) /* pointer with structure */

p->price=p->price+10;
}

Output:
Enter your icode, price and name :
107
27.50
LuxSoap

Before price increment :


Item Code : 107
Item Name : LuxSoap
Item Price : 27.500000

After price increment :


Item Code : 107
Item Name : LuxSoap
Item Price : 37.500000

File:
A file represents a sequence of bytes on the disk where a group of related data is stored. File is
created for permanent storage of data. It is a readymade structure.

Operations on File:

 Naming a file
 Opening a file
 Reading from a file
 Writing to a file

MS Page 9
UNIT – V Programming in C

 Closing a file

Creating a FILE pointer:

For store and retrieve information in a file we need to create a FILE pointer. In C language we can
create FILE pointer as follows

Syntax:

FILE *fptr;

Ex:

FILE *fp;

Opening a file:

The fopen() function is used to create a new file or to open an existing file.

Syntax:

fptr = fopen(filename, mode);

Here filename is the name of the file to be opened and mode specifies the purpose of opening the file.

Ex:

fp=fopen(“myfile.txt”,”w”);

File opening modes:

File opening mode specifies the purpose of file to be opened. C language supports the following file
opening modes.

mode Description

r opens a text file in reading mode

w opens or create a text file in writing mode.

MS Page 10
UNIT – V Programming in C

a opens a text file in append mode

r+ opens a text file in both reading and writing mode

w+ opens a text file in both reading and writing mode

a+ opens a text file in both reading and writing mode

rb opens a binary file in reading mode

wb opens or create a binary file in writing mode

ab opens a binary file in append mode

rb+ opens a binary file in both reading and writing mode

wb+ opens a binary file in both reading and writing mode

ab+ opens a binary file in both reading and writing mode

Closing a file:

The fclose() function is used to close an already opened file.

Syntax :
fclose(fptr );

Ex:
fclose(fp);
Write a C program to open a text file abc and close it.

#include<stdio.h>

MS Page 11
UNIT – V Programming in C

#include<conio.h>

void main()

{
FILE *fp; /* creating file pointer */
clrscr();
fp=fopen("abc.txt","w"); /* opening a file */

if(fp==NULL)
{

printf("\n The file cannot be open");


getch();
exit(10);
}
fclose(fp); /* closing a file */
getch();

}
Input and output operations on Files:

putc():

This function is used to write a single character into a file.

Syntax:

putc(charvar,fptr);

Ex:

putc(c,fp);

This function will write a character in the character variable c to the file, opened by the file pointer fp.

Example:

MS Page 12
UNIT – V Programming in C

#include<stdio.h>
#include<conio.h>

void main()
{
FILE *fp;
char c;
clrscr();
fp=fopen("xyz.txt","w");

if(fp==NULL)
{
printf("\nThis file can not be open");
getch();
exit(3);
}

while((c=getchar())!='@') /* repeat until the character @ */


putc(c,fp); /* write a character into xyz.txt */
fclose(fp);
}

Output:
s
a
t
h
i
s

MS Page 13
UNIT – V Programming in C

h
@
getc():

This function is used to read a single character from a file.

Syntax:

charvar=getc(fptr);

Ex:

c=getc(fp);

Example:

#include<stdio.h>
#include<conio.h>

void main()
{

FILE *fp;
char c;
clrscr();
fp=fopen("xyz.txt","r");

if(fp==NULL)
{

printf("\nThis file can not be open");


getch();
exit(3);
}

MS Page 14
UNIT – V Programming in C

while((c=getc(fp))!=EOF)
printf("%c",c);

fclose(fp);
getch();
}

Output:
s
a
t

h
i
s
h

getw() and putw():


the getw() and putw() are integer-oriented functions. They are similar to the getc() and putc()
functions and are used to read and write integer values.

putw(integerval,fptr);

integervar=getw(fptr);

fprintf()

This function is used to read all kinds of data from a file.

Syntax:

fprintf(fptr,”controlstring”,variablelist);

Ex:

fprintf(fp,” %d %s %f”, no,name,sal);

MS Page 15
UNIT – V Programming in C

Example:

#include<stdio.h>
#include<conio.h>

void main()

{
FILE *fp;
int no;
float sal;
char name[30],ch='y';
clrscr();

fp=fopen("edata.dat","w");

if(fp==NULL)
{
printf("The file cannot be open");

getch();
exit(9);
}

while(ch=='y')
{
printf("Enter your no,name and salary :\n");

scanf("%d %s %f",&no,name,&sal);
fprintf(fp," %d %s %f \n",no,name,sal);
printf("Do you want to continue[y/n] :");
scanf(" %c",&ch);

MS Page 16
UNIT – V Programming in C

}
fclose(fp);
}

fscanf():

MS Page 17

You might also like