Unit 5
Unit 5
char nm[20];
struct day d;
Lecture Note: 25
We can pass each element of the structure through function but passing individual
element is difficult when number of structure element increases. To overcome this,
we use to pass the whole structure through function instead of passing individual
element.
#include<stdio.h>
#include<string.h>
void main()
struct student
char name[30];
char branch[25];
int roll;
}struct student s;
printf("\nEnter roll:");
scanf("%d",&s.roll);
printf("\nEnter branch:");
gets(s.branch);
display(name,roll,branch);
#include<stdio.h>
#include<string.h>
struct student
char name[30];
int age,roll;
};
void main()
108 *Under revision
{
display(s1);
display(s2);
display(struct student s)
Output: name=sona
roll=16
Lecture Note: 26
UNION
Union is derived data type contains collection of different data type or dissimilar
elements. All definition declaration of union variable and accessing member is
similar to structure, but instead of keyword struct the keyword union is used, the
main difference between union and structure is
Where union offers a memory treated as variable of one type on one occasion
where (struct), it read number of different variables stored at different place of
memory.
Syntax of union:
union student
datatype member1;
datatype member2;
};
Like structure variable, union variable can be declared with definition or separately
such as
Datatype member1;
}var1;
Union members can also be accessed by the dot operator with union variable and if
we have pointer to union then member can be accessed by using (arrow) operator
as with structure.
struct student
int i;
char ch[10];
};struct student s;
Lecture Note:27
Nested of Union
When one union is inside the another union it is called nested of union.
Example:-
union a
int i;
int age;
};
union b
char name[10];
union a aa;
}; union b bb;
Example:-
void main()
struct a
int i;
char ch[20];
};
struct b
int i;
char d[10];
};
union z
struct a a1;
struct b b1;
112 *Under revision
}; union z z1;
z1.b1.j=20;
z1.a1.i=10;
z1.a1.ch[10]= “ i“;
z1.b1.d[0]=”j “;
printf(“ “);
If number of values to be stored is less than the size of memory, there would be
wastage of memory.
If we would want to store more values by increase in size during the execution on
assigned size then it fails.
Allocation and release of memory space can be done with the help of some library
function called dynamic memory allocation function. These library function are
called as dynamic memory allocation function. These library function prototype
are found in the header file, “alloc.h” where it has defined.
Function take memory from memory area is called heap and release when not
required.
Pointer has important role in the dynamic memory allocation to allocate memory.
malloc():
malloc ()
returns the pointer to the 1st byte and allocate memory, and its return type is void,
which can be type cast such as:
int *p=(datatype*)malloc(size)
If memory location is successful, it returns the address of the memory chunk that
was allocated and it returns null on unsuccessful and from the above declaration a
pointer of type(datatype) and size in byte.
And datatype pointer used to typecast the pointer returned by malloc and this
typecasting is necessary since, malloc() by default returns a pointer to void.
Example int*p=(int*)malloc(10);
So, from the above pointer p, allocated IO contigious memory space address of 1st
byte and is stored in the variable.
We can also use, the size of operator to specify the the size, such as
*p=(int*)malloc(5*size of int) Here, 5 is the no. of data.
Example:
void main()
int n , avg,i,*p,sum=0;
scanf(“%d”,&n);
p=(int *)malloc(n*size(int));
if(p==null)
printf(“not sufficient”);
exit();
for(i=0;i<n;i++)
scanf(“%d”,(p+i));
for(i=0;i<n;i++)
Printf(“%d”,*(p+i));
sum=sum+*p;
avg=sum/n;
printf(“avg=%d”,avg);
Lecture Note: 28
calloc()
Similar to malloc only difference is that calloc function use to allocate multiple
block of memory .
Example:-
realloc()
The function realloc use to change the size of the memory block and it alter the
size of the memory block without loosing the old data, it is called reallocation of
memory.
If new size is larger than the old size, then old data is not lost and newly allocated
bytes are uninitialized. If old address is not sufficient then starting address
contained in pointer may be changed and this reallocation function moves content
of old block into the new block and data on the old block is not lost.
Example:
#include<stdio.h>
#include<alloc.h>
void main()
int i,*p;
if(p==null)
exit();
printf(“enter 5 integer”);
for(i=0;i<5;i++)
scanf(“%d”,(p+i));
int*ptr=(int*)realloc(9*size of (int) );
if(ptr==null)
printf(“not available”);
exit();
for(i=5;i<9;i++)
scanf(“%d”,(p+i));
for(i=0;i<9;i++)
printf(“%d”,*(p+i));
free()
117 *Under revision
Function free() is used to release space allocated dynamically, the memory
released by free() is made available to heap again. It can be used for further
purpose.
void(*ptr)
Or
free(p)
Lecture Note: 29
Dynamic array
Subscript notation
Pointer notation
Example:
#include<alloc.h>
void main()
scanf(“%d”,&n);
p=(int*)malloc(n*size of int);
If(p==null)
exit();
for(i=0;i<n;i++)
printf(“enter an integer”);
scanf(“%d”,&p[i]);
for(i=0;i<n;i++)
printf(“%d”,p[i]);
File handling
119 *Under revision
File: the file is a permanent storage medium in which we can store the data
permanently.
File Operation
opening a file:
Before performing any type of operation, a file must be opened and for this
fopen() function is used.
syntax:
example:
FILE *fp=fopen(“ar.c”,”r”);
If fopen() unable to open a file than it will return NULL to the file pointer.
File-pointer: The file pointer is a pointer variable which can be store the address
of a special file that means it is based upon the file pointer a file gets opened.
FILE* var;
Modes of open
Syntax:
character_variable=getc(file_ptr);
puts(character-var,file-ptr);
ClOSING A FILE
fclose(file-ptr);
File Operation
(3)writing a file
(4)closing a file
121 *Under revision
Before performing any type of operation we must have to open the file.c, language
communicate with file using A new type called file pointer.
If fopen() unable to open a file then it will return NULL to the file-pointer.
Lecture Note: 30
Syntax:
Syntax:
fputc(character,file_pointer);
#include<stdio.h>
void main()
FILE *fs,*fd;
char ch;
If(fs=fopen(“scr.txt”,”r”)==0)
return;
If(fd=fopen(“dest.txt”,”w”)==0)
fclose(fs);
return;
while(ch=fgets(fs)!=EOF)
fputc(ch,fd);
fcloseall();
Syntax:
gets(file pointer);
Syntax:
fputs(integer,file_pointer);
#include<stdio.h>
#include<stdlib.h>
void main()
FILE *fp;
int word;
fp=fopen(“dgt.txt”,”wb”);
If(fp==NULL)
exit(1);
word=94;
putw(word,fp);
If(ferror(fp))
124 *Under revision
printf(“Error writing to file\n”);
else
printf(“Successful write\n”);
fclose(fp);
fp=fopen(“dgt.txt”,”rb”);
If(fp==NULL)
exit(1);
word=getw(fp);
If(ferror(fp))
else
printf(“Successful read:word=%d\n”,word);
/*clean up*/
fclose(fp);
Lecture Note: 31
Syntax:
Syntax:
fputs(string,file_pointer);
#include<string.h>
#include<stdio.h>
void main(void)
FILE*stream;
char msg[20];
stream=fopen(“DUMMY.FIL”,”w+”);
fwrite(string,strlen(string),1,stream);
fseek(stream,0,SEEK_SET);
fgets(msg,strlen(string)+1,stream);
printf(“%s”,msg);
fclose(stream);
BOOKS: