Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CNotes 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

Unit - II

---------
Arrays
Strings
Structures
Unions
Pointers
Enumerations

Arrays:
An array is a group of homogeneous data elements stored in continuous memory locations under
a single name.
(or)
An array is a collections similar types of data elements stored in continuous memory location
under a single name.

types of arrays
1. One dimensional array(1-D Array)
2. Two dimensional array(2-D Array)

1. One dimensional array(1-D Array):


We can declare the 1-D array as follows

syntax:
datatype arrayname[size];

here size indicates the number of elements to be stored in the array

Ex:
int rno[5];

Initialization of array:
we can initialize the array elements at the time of decleration as follows.

int rno[5]={501,502,503,504,505};

To access the elements of array we can use the array index as follows.

Always the array index starts with zero and ends at arraysize - 1.

array size =5
last index =5-1 =4

syntax:
arrayname[index]
rno[0] => To access first element of array
rno[1] => To access second element of array
rno[2] => To access third element of array
rno[3] => To access fourth element of array
rno[4] => To access fifth element of array

The Memory required to store an array is


total memory = size of data type(bytes) * size of array
total memory of rno array = 2-bytes * 5 = 10-bytes

Ex:2

float avg[3]={99.4,99.3,80.0};

avg[0];
avg[1];
avg[2];

total memory for avg array = 4-bytes * 3 = 12-bytes

Write a program to demonstrate the decleration,initialization and accessing of 1-D array.


array1.c

#include<stdio.h>
main()
{
int rno[5]={501,502,503,504,505};
int i;
clrscr();
printf("The elements of array are\n");
for(i=0;i<5;i++)
{
printf("%d\n",rno[i]);
}
return 0;
}
output:
Write a program to create a float array and display its elements.
array2.c

#include<stdio.h>
main()
{
float avg[3]={90.67,45.68,89.45};
int i;
clrscr();
printf("The elements of array are\n");
for(i=0;i<3;i++)
{
printf("%.2f\n",avg[i]);
}
return 0;
}
output:

Write a program to create an array of marks of a class for one subject and display the marks of
marks array.
array3.c

#include<stdio.h>
main()
{
int marks[10],i;
clrscr();
printf("Enter the marks of 10 students\n");
for(i=0;i<10;i++)
{
scanf("%d",&marks[i]);
}
printf("The elements of marks array are\n");
for(i=0;i<10;i++)
{
printf("%d\n",marks[i]);
}
return 0;
}
output:
Write a program to calculate the avg marks of a marks array.
array4.c

#include<stdio.h>
main()
{
int marks[10],i,sum=0;
float avg;
clrscr();
printf("Enter the marks of 10 students\n");
for(i=0;i<10;i++)
{
scanf("%d",&marks[i]);
}
printf("The elements of marks array are\n");
for(i=0;i<10;i++)
{
printf("%d\n",marks[i]);
sum=sum+marks[i];
}
avg=sum/10.0;
printf("Average marks are %f\n",avg);
return 0;
}
output:

2-Dimensional Arrays(2D Arraty):


________________________________

A 2 dimensional array is a combination of some number of 1 dimensional arrays.

A 2D array is declared with 2 subscripts to specify the rows size and columns size.

syntax:
datatype arrayname[rowsize][columnsize];

ex:
int matrix[3][3];

Initialization of 2D array:

We can initialize the elements of 2D array at the time of decleration as follows.

int matrix[3][3]={10,20,30,40,50,60,70,80,90};
(or)
int matrix[3][3]={{10,20,30},{40,50,60},{70,80,90}};

Note: When declaring and initializing the 2D array we must mention the column size.
Accessing the elements of 2D array:
we can access the 2D array elements using row and column index within the subscripts.
row index always starts at zero(0) and ends at rowsize-1.
column index always starts at zero(0) and ends at columnsize-1.

To access first row,first column element we use the index matrix[0][0].


To access first row,third column element we use the index matrix[0][2].
To access second row,second column element we use the index matrix[1][1].
To access third row,third column element we use the index matrix[2][2].

The memory allocation for 2D array:


memory size= rowsize * columnsize * datatypesize
memory size=3 * 3 * 2-bytes = 18-bytes

The memory for 2D array is allocated in the sequential memory locations.

Write a program to declare,initialize a 2D array and display it in matrix format.


array2d1.c

#include<stdio.h>
main()
{
int matrix[3][3]={10,20,30,40,50,60,70,80,90};
int i,j;
clrscr();
printf("The matrix elements are\n");
for(i=0;i<3;i++) /*outer loop is for rows iteration*/
{
for(j=0;j<3;j++) /*inner loop is for columns iteration*/
{
printf("%d\t",matrix[i][j]);
}
printf("\n");
}
return 0;
}

output:

Write a program to calculate the addition of 2 matrices.

array2d2.c

#include<stdio.h>
#define SIZE 10
main()
{
int mat1[SIZE][SIZE],mat2[SIZE][SIZE],mat3[SIZE][SIZE];
int r1,r2,c1,c2,i,j;
clrscr();
printf("Enter r1,c1 of matrix1\n");
scanf("%d%d",&r1,&c1);
printf("Enter the elements of matrix1\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Enter r2,c2 of matrix2\n");
scanf("%d%d",&r2,&c2);
printf("Enter the elements of matrix2\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&mat2[i][j]);
}
}
if(r1==r2 && c1==c2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
}
printf("The Addition of 2 matrices is\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d\t",mat3[i][j]);
}
printf("\n");
}
}
else
{
printf("Matrix addition is not possible\n");
}
return 0;
}

output:
HW1:
Write a program to calculate the subtraction of 2 matrices.

Write a program to calculate the multiplication of 2 matrices.

array2d3.c

#include<stdio.h>
#define SIZE 10
main()
{
int mat1[SIZE][SIZE],mat2[SIZE][SIZE],mat3[SIZE][SIZE];
int r1,r2,c1,c2,i,j,k;
clrscr();
printf("Enter r1,c1 of matrix1\n");
scanf("%d%d",&r1,&c1);
printf("Enter the elements of matrix1\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Enter r2,c2 of matrix2\n");
scanf("%d%d",&r2,&c2);
printf("Enter the elements of matrix2\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&mat2[i][j]);
}
}
if(c1==r2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
mat3[i][j]=0;
for(k=0;k<c1;k++)
{
mat3[i][j]=mat3[i][j]+mat1[i][k]*mat2[k][j];
}
}
}
printf("The Multiplication of 2 matrices is\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",mat3[i][j]);
}
printf("\n");
}
}
else
{
printf("Matrix Multiplication is not possible\n");
}
return 0;
}

output:

HW2:
Write a program to display the transposition of a given matrix
************************ Strings ********************
-----------------------------------------------------
A string is a collection of characters
(or)
An array of characters is known as a string.

Decleration of a string:

We can declare a string using char datatype as follows.

syntax:
char stringname[size];

ex:
char name[15];

Initialization of a string:

We can initialize the string at the time of decleration as follows.

ex:

char name[15]={'S','u','m','a','t','h','i',' ','R','e','d','d','y','\0'};


(or)
char name[15]="Sumathi Reddy";

Access the elements of a string:


We use the index to access the individual elements of a string. The index begins with zero and
ends at size - 1.

The memory required to allocate for a string.

Memory = size of string * size of datatype


= 15 * 1-byte
= 15-bytes

Note: The last character of the string must be the null character (\0). We indicate the null
character using \0 [slash zero].
Write a program to declare, initialize and display the string.
(display using printf() with format specifier %c)

str1.c

#include<stdio.h>
main()
{
char name[15]={'S','u','m','a','t','h','i',' ','R','e','d','d','y','\0'};
int i;
clrscr();
printf("The string is\n");
for(i=0;name[i]!='\0';i++)
{
printf("%c",name[i]);
}
return 0;
}
output:

Write a program to declare, initialize and display the string.


(display using printf() with format specifier %s)

str2.c

#include<stdio.h>
main()
{
char name[15]={'S','u','m','a','t','h','i',' ','R','e','d','d','y','\0'};

clrscr();
printf("The string is\n");
printf("%s",name);

return 0;
}

output:
Write a program to declare, initialize and display the string.
(display using puts)

str3.c

#include<stdio.h>
main()
{
char name[15]={'S','u','m','a','t','h','i',' ','R','e','d','d','y','\0'};

clrscr();
printf("The string is\n");
puts(name);

return 0;
}

output:

Write a program to calculate the length of a given string

str4.c

#include<stdio.h>
main()
{
char name[15]={'S','u','m','a','t','h','i',' ','R','e','d','d','y','\0'};
int i,len=0;
clrscr();
for(i=0;name[i]!='\0';i++)
{
len++;
}
printf("The string is\n");
puts(name);
printf("Length is %d\n",len);
return 0;
}
output:
Write a program to copy one string into other string

str5.c

#include<stdio.h>
main()
{
char name[15]={'S','u','m','a','t','h','i',' ','R','e','d','d','y','\0'};
char name1[15];
int i;
clrscr();
for(i=0;name[i]!='\0';i++)
{
name1[i]=name[i];
}
name1[i]='\0';
printf("The string name is \n");
puts(name);
printf("The string name1 is \n");
puts(name1);
return 0;
}

output:

Write a program to display the string in reverse

str6.c

#include<stdio.h>
main()
{
char name[15]={'S','u','m','a','t','h','i',' ','R','e','d','d','y','\0'};
int i,len=0;
clrscr();
for(i=0;name[i]!='\0';i++)
{
len++;
}

printf("The string in reverse is\n");


for(i=len-1;i>=0;i--)
{
printf("%c",name[i]);
}
return 0;
}

output:

Write a program to concatenate one string at the end of other string.

str7.c

#include<stdio.h>
main()
{
char str1[20]={'R','a','j','\0'};
char str2[10]={'e','s','h','\0'};
int i,len1=0,len2=0;
clrscr();
printf("Before concatenation\n");
printf("str1 is %s\n",str1);
printf("str2 is %s\n",str2);
for(i=0;str1[i]!='\0';i++)
{
len1++;
}
for(i=0;str2[i]!='\0';i++)
{
len2++;
}
for(i=0;i<len2;i++)
{
str1[i+len1]=str2[i];
}
str1[i+len1]='\0';
printf("After Concatenation\n");
printf("str1 is %s\n",str1);
printf("str2 is %s\n",str2);
return 0;
}

output:

String functions:
string functions are available in string.h header library in c language.

The following are the string functions


1. strlen()
2. strrev()
3. strcpy()
4. strncpy()
5. strcat()
6. strncat()
7. strcmp()
8. strncmp()

1. strlen():
This function will returns the length of a given string

syntax:
int var=strlen(str);

2. strrev():
This function will reverses the given string.
syntax:
strrev(str);

3. strcpy():
This functions copies one string into other string.

syntax:
strcpy(dest,src);

4. strncpy():
This function will copies the specified number of characters of one string into other string.

syntax:
strncpy(dest,src,number);

5. strcat():
This function will concatenate one string at the end of other string.

syntax:
strcat(str1,str2)

6. strncat():
This function will concatenate the specified number of characters of one string with other string.

syntax:
strncat(str1,str2,number);

7. strcmp():
This function will compares 2 strings and returns zero if the strings are equal, returns positive or
negative number if the 2 strings are not equal.

This function follows the ascii codes to compare the strings.

syntax:
int var=strcmp(str1,str2);

8. strncmp():
This function will compares the specified number of characters of 2 string and returns zero if 2
strings are equal, returns positive or negative number if the 2 strings are not equal.

syntax:
int var=strncmp(str1,str2,number);

write a program to demonstrate strlen() function.

str8.c
#include<stdio.h>
#include<string.h>
main()
{
char str[20]="Sumathi Reddy";
int len;
clrscr();
len=strlen(str);
printf("The length of %s is %d\n",str,len);
return 0;
}

output:

Write a program to demonstrate strrev() function.

str9.c

#include<stdio.h>
#include<string.h>
main()
{
char str[20]="Sumathi Reddy";
clrscr();
printf("The string is %s\n",str);
printf("The string in reverse is %s\n",strrev(str));
return 0;
}
output:

Write a program to demonstrate strcpy() and strncpy() functions.

str10.c

#include<stdio.h>
#include<string.h>
main()
{
char str[20]="Sumathi Reddy";
char str1[20],str2[20];
clrscr();
strcpy(str1,str);
strncpy(str2,str,7);

printf("The string str is %s\n",str);


printf("The string str1 is %s\n",str1);
printf("The string str2 is %s\n",str2);
return 0;
}

output:

Write a program to demonstrate strcat() and strncat() functions.

str11.c

#include<stdio.h>
#include<string.h>
main()
{
char str[30]="Sumathi ";
char str1[20]="Reddy";
char str2[20]=" College for";
clrscr();
printf("The string str is %s\n",str);
printf("The string str1 is %s\n",str1);
printf("The string str2 is %s\n",str2);

strcat(str,str1);
printf("The string str after concatenation with str1 %s\n",str);
strncat(str,str2,8);
printf("The string str after concatenation with str2 is %s\n",str);
return 0;
}

output:
Write a program to demonstrate strcmp() and strncmp() functions.

str12.c

#include<stdio.h>
#include<string.h>
main()
{
char str[30]="Sumathi ";
char str1[20]="Suma";
int c1,c2;
clrscr();
c1=strcmp(str,str1);
if(c1==0)
{
printf("The strings are equal\n");
}
else
{
printf("The string are not equal\n");
}
c2=strncmp(str,str1,4);
if(c2==0)
{
printf("first 4 characters of 2 strings are equal\n");
}
else
{
printf("first 4 characters of 2 strings are not equal\n");
}
return 0;
}

output:
Write a program to read a string and display it.

str13.c

#include<stdio.h>
main()
{
char str[50];
printf("Enter a string\n");
scanf("%s",str);
printf("The string entered is %s\n",str);
return 0;
}

output:

Note: When we read the string using scanf() with format specifier %s it does not allow the white
space characters(space,new line, tab), hence to over come from this problem we should use get()
to read the string.

Re-write the above program using gets().

str14.c

#include<stdio.h>
main()
{
char str[50];
clrscr();
printf("Enter a string\n");
gets(str);
printf("The string entered is %s\n",str);
return 0;
}

output:
Arrays of strings:
We can create the arrays of strings to store more strings.

We can crate arrays of strings as follows.

syntax:
char stringname[size][size]

first subscript indicates the number of strings.


second subscript indicates the maximum length of each string.

ex:
char names[5][15];

Initialization of arrays of strings:

char names[5][15]={"Rajesh","Ramesh","Rahul","Ramu","Rohan"};

we can access each string using the index

Write a program to create an array strings, initialize and display them.

str15.c

#include<stdio.h>
main()
{
char names[5][15]={"Rajesh","Ramesh","Rahul","Ramu","Rohan"};
int i;
clrscr();
printf("The strings are\n");
for(i=0;i<5;i++)
{
printf("%s\n",names[i]);
}
return 0;
}

output:

*********************** Structures *****************************


-----------------------------------------------------------------

Structure is a user defined datatype, it is a collection of different types of data elements stored
under a single name.
(or)
Structure is a used defined datatype, it is a collecion of dissimilar(heterogenous) types of data
elements stored under a single name.

To create and access the structures we need to follow the following steps.

1. structure template creation


2. structure variable creation
3. accessing the structure members

1. structure template creation:


we can create the structure template using the keyword struct as follows

syntax:

struct structname
{
datatype member1;
datatype member2;
datatype member3;
.
.
datatype membern;
};

Ex;

struct student
{
int rno;
char name[20];
float avg;
};

2. structure variable creation:


Once we create a structure template we should create a variable to the structure to access the
members of a structure.

We can create the structure variable as follows.

syntax:
struct structname variablename;

ex:
struct student s1;

3. accessing the structure members:


To access the structure we should use the member operator(.)(dot operator) as follows.

syntax:
variablename.member;

ex:

s1.rno;
s1.name;
s1.avg;

Write a program to create,initialize and access the members of a structure.

st1.c

#include<stdio.h>
main()
{
struct student
{
int rno;
char name[15];
float avg;
};
struct student s1={10,"Rajesh",7.9};
clrscr();
printf("The members of structure are\n");
printf("Rno : %d\n",s1.rno);
printf("Name : %s\n",s1.name);
printf("Avg : %f\n",s1.avg);
return 0;
}

output:

We can also declare the variable of structure at the time of structure template creation as follows.

struct structname
{
datatype member1;
datatype member2;
..
..
datatype membern;
}variable;

Write a program to demonstrate the structure variable creation with structure template.

st2.c

#include<stdio.h>
main()
{
struct student
{
int rno;
char name[15];
float avg;
}s1={10,"Rajesh",7.9};
clrscr();
printf("The members of structure are\n");
printf("Rno : %d\n",s1.rno);
printf("Name : %s\n",s1.name);
printf("Avg : %f\n",s1.avg);
return 0;
}
output:

we can also create more than one variable to the structure as per the requirement.

Write a program to create more variables to the structure.

st3.c

#include<stdio.h>
main()
{
struct student
{
int rno;
char name[15];
float avg;
};
struct student s1={10,"Rajesh",7.9};
struct student s2={11,"Ramesh",9.78};
struct student s3={12,"Sachin",5.678};

clrscr();
printf("The student1 details are\n");
printf("Rno : %d\n",s1.rno);
printf("Name : %s\n",s1.name);
printf("Avg : %f\n",s1.avg);

printf("The student2 details are\n");


printf("Rno : %d\n",s2.rno);
printf("Name : %s\n",s2.name);
printf("Avg : %f\n",s2.avg);

printf("The student3 details are\n");


printf("Rno : %d\n",s3.rno);
printf("Name : %s\n",s3.name);
printf("Avg : %f\n",s3.avg);

return 0;
}
output:

Array of structures:
We can create an array variable to the structure to store more information.

syntax:
struct structname variable[size];

Write a program to create an array structure for storing 5 students details and display the same
details.

st4.c

#include<stdio.h>
void dummy(float *a)
{
float b=*a;
dummy(&b);
}
main()
{
struct student
{
int rno;
char name[15];
float avg;
};
struct student s[5];
int i;
clrscr();
for(i=0;i<5;i++)
{
printf("Enter the student details rno,name,avg\n");
scanf("%d",&s[i].rno);
scanf("%s",s[i].name);
scanf("%f",&s[i].avg);
}

for(i=0;i<5;i++)
{
printf("The student%d details are\n",i+1);
printf("Rno : %d\n",s[i].rno);
printf("Name : %s\n",s[i].name);
printf("Avg : %f\n\n",s[i].avg);
}

return 0;
}
output:
HW1:
Write a program to create a structure employee with the members eid,ename and esalary, create
an array to employee structure for storing 20 employees details and display them.
HW2:
Write a program to create a structure book with the members bookid,bookname,bauthor and
bookprice, create an array to book structure for storing 5 books details and display them.
HW3:
Write a program to create a structure item with the members itemid,itemname and price, create
an array to item structure for storing 10 items and display them.

Nested structure:
defining a structure inside another structure is known as nested structure

syntax:
struct outstructname
{
datatype member1;
datatype member2;
struct instructname
{
datatype member1;
datatype member2;
.....
}ivar;
}ovar;

ex:
struct person
{
char name[15];
struct date
{
int day;
int month;
int year;
}dob;
}p;

To access the members of nested structure we can use the inside structure variable with the
outside structure variable

syntax:
ovar.ivar.member1;

Ex:
p.dob.day;
p.dob.month;
p.dob.year;

Write a program to demonstrate the nested structure for displaying a person details.

st5.c

#include<stdio.h>
main()
{
struct person
{
char name[15];
struct date
{
int day;
int month;
int year;
}dob;
}p;
clrscr();
printf("Enter name,dob(dd,mon,year) of a person\n");
scanf("%s",p.name);
scanf("%d%d%d",&p.dob.day,&p.dob.month,&p.dob.year);
printf("The details of a person\n");
printf("Name : %s\n",p.name);
printf("DOB : %d-%d-%d\n",p.dob.day,p.dob.month,p.dob.year);
return 0;
}

output:

union:
union is defined similar to the structure using the keyword union

a union will allocate the memory to only one member at a time.


the maximum member size memory will be allocated to any member of the union.
The member for which the memory is allocated is known as active member.

we can define a union using the keyword union as follows.

syntax:
union unionname
{
datatype member1;
datatype member2;
datatype member3;
..
..
datatype membern;
};

ex:
union student
{
int rno;
char name[15];
float avg;
};

union student s;
Write a program to demonstrate the union.

un1.c

#include<stdio.h>
#include<string.h>
main()
{
union student
{
int rno;
char name[15];
float avg;
}u;
clrscr();
u.rno=15;
printf("Rno : %d\n",u.rno);
strcpy(u.name,"Prashanth");
printf("Name : %s\n",u.name);
u.avg=4.567;
printf("Avg : %f\n",u.avg);

return 0;
}
output:
Write a program to show the memory size allocated for a union and structure.

un2.c

#include<stdio.h>
#include<string.h>
main()
{
union student
{
int rno;
char name[15];
float avg;
}u;
struct student1
{
int rno;
char name[15];
float avg;
}s;
clrscr();
printf("The memory size %d bytes of union\n",sizeof(union student));
printf("The memory size %d bytes of structure\n",sizeof(struct student1));
return 0;
}
output:

enumerations:

An enumeration is a user defined datatype, we use it to define the symbolic constants or


numerical constants.

We can define the enumeration with the keyword enum as follows.

syntax:
enum tagname{member1,member2,member3,....,membern}var1,var2,var3;

In the above syntax either tagname or variable names or both are optional.

ex:
enum days{sun,mon,tue,wed,thu,fri,sat}start,end;

The members of enum will be assigned with the numerical values. by default the first member
will be assigned zero and subsequent members will be assigned by increasing one each.

as per above example the assigned values for enum days are

sun -> 0
mon -> 1
tue -> 2
wed -> 3
thu -> 4
fri -> 5
sat -> 6

start=sun; -> 0
end=sat; -> 6

If the programmer wants to initialize the values then we can do it by initializing to the members
of enum

ex:
enum days{sun=1,mon,tue,wed,thu,fri,sat}start,end;

If the programmer initializes to any member in the enum then the subsequent members will be
assigned with increased by one each
as per above example the assigned values for enum days are

sun -> 1
mon -> 2
tue -> 3
wed -> 4
thu -> 5
fri -> 6
sat -> 7

start=sun; -> 1
end=sat; -> 7

If the programmer wants to initialize to any member in the middle then the programmer can do
so.

ex:
enum days{sun=1,mon,tue,wed=45,thu,fri,sat}start,end;

sun -> 1
mon -> 2
tue -> 3
wed -> 45
thu -> 46
fri -> 47
sat -> 48

start=sun; -> 1
end=sat; -> 48

Write a program to demonstrate the creation of enumeration for days(with default values)

enum1.c

#include<stdio.h>
main()
{
enum days{sun,mon,tue,wed,thu,fri,sat}start,end;
clrscr();
start=sun;
end=sat;
printf("The start day number of a week is %d\n",start);
printf("The end day number of a week is %d\n",end);
return 0;
}
output:

Write a program to demonstrate the creation of enumeration for days(with initialization to first
member)

enum2.c

#include<stdio.h>
main()
{
enum days{sun=1,mon,tue,wed,thu,fri,sat}start,end;
clrscr();
start=sun;
end=sat;
printf("The start day number of a week is %d\n",start);
printf("The end day number of a week is %d\n",end);
return 0;
}
output:

Write a program to demonstrate the creation of enumeration for days(with initialization to any
middle member)

enum3.c

#include<stdio.h>
main()
{
enum days{sun=1,mon,tue,wed=45,thu,fri,sat}start,end;
clrscr();
start=sun;
end=sat;
printf("Sunday %d\n",sun);
printf("Monday %d\n",mon);
printf("Tuesday %d\n",tue);
printf("Wednesday %d\n",wed);
printf("Thursday %d\n",thu);
printf("Friday %d\n",fri);
printf("Saturday %d\n",sat);
printf("The start day number of a week is %d\n",start);
printf("The end day number of a week is %d\n",end);
return 0;
}
output:

Write a program to create an enumeration for months and display the numbers of each month

enum4.c

#include<stdio.h>
main()
{
enum months{jan=1,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec};
clrscr();
printf("January %d\n",jan);
printf("February %d\n",feb);
printf("March %d\n",mar);
printf("April %d\n",apr);
printf("May %d\n",may);
printf("June %d\n",jun);
printf("July %d\n",jul);
printf("August %d\n",aug);
printf("September %d\n",sep);
printf("October %d\n",oct);
printf("November %d\n",nov);
printf("December %d\n",dec);

return 0;
}
output:
Write a program to create an enumeration for scores and assign the scores to some players then
display the player and score.

enum5.c

#include<stdio.h>
main()
{
enum scores{duck,half_cen=50,cen=100,d_cen=200}rohith,virat,boomra,dhavan;
clrscr();
dhavan=half_cen;
virat=cen;
rohith=d_cen;
boomra=duck;

printf("The scores of players are\n");


printf("Dhavan %d\n",dhavan);
printf("Virat %d\n",virat);
printf("Rohith %d\n",rohith);
printf("Boomra %d\n",boomra);
return 0;
}
output:

HW1:
Write a program to create an enumeration of weights and display the created weights

*******************************************************
---------------------- Pointers -----------------------
*******************************************************

A pointer is a variable which stores the address of other variable.

Uses of a pointers:
We can access the variables indirectly using pointers.
We can create the dynamic memory allocations using pointers.
We can access the variable fast using the pointers.

Decleration of a pointer:

We can declare a pointer using *(pointer) operator preceeding to the variable name.

syntax:
datatype *pointername;

ex:
int *iptr;
float *fptr;
char *cptr;

We can assign the address of other variables to the pointer using the address(&) operator

syntax:
pointername=&variable;

int i;
float f;
char c;
i=10;
f=4.567;
c='x';

iptr=&i;
fptr=&f;
cptr=&c;
Write a program to demonstrate the creation of a pointers, assigning the address of a variable to
the pointer and display the address of a variable using pointer.

ptr1.c

#include<stdio.h>
main()
{
int *iptr,i;
float *fptr,f;
char *cptr,c;
clrscr();
i=10;
f=4.567;
c='x';
iptr=&i;
fptr=&f;
cptr=&c;

printf("Address of i through iptr is %u\n",iptr);


printf("Address of f through fptr is %u\n",fptr);
printf("Address of c through cptr is %u\n",cptr);

return 0;
}
output:
Indirection Operator(*):
We can access the value at address using indirection(value stored at address/dereference)
operator (*) with the pointer name.

Write a program to display the values of a variables using pointers with values stored at address
operator.

ptr2.c

#include<stdio.h>
main()
{
int *iptr,i;
float *fptr,f;
char *cptr,c;
clrscr();
i=10;
f=4.567;
c='x';
iptr=&i;
fptr=&f;
cptr=&c;

printf("Address of i through iptr is %u\n",iptr);


printf("Address of f through fptr is %u\n",fptr);
printf("Address of c through cptr is %u\n",cptr);

printf("The value of i using *iptr is %d\n",*iptr);


printf("The value of f using *fptr is %f\n",*fptr);
printf("The value of c using *cptr is %c\n",*cptr);

return 0;
}
output:
Pointer to Pointer:

A pointer to pointer is a variable which stores the address of a pointer.

Decleration of a pointer to pointer:

We can declare the pointer to pointer using **(pointer to pointer) operator preceeded with the
pointername.

syntax:
datatype **pointertopointername;

ex:
int **piptr;
float **pfptr;
char **pcptr;

We can assign the address of a pointer to pointer to pointer using the address(&) operator.

piptr=&iptr;
pfptr=&fptr;
pcptr=&cptr;
Write a program to create a pointer-to-pointer, assign the address of a pointer to pointer-to-
pointer and display address of a pointer using pointer-to-pointer

ptr3.c

#include<stdio.h>
main()
{
int **piptr,*iptr,i;
float **pfptr,*fptr,f;
char **pcptr,*cptr,c;
clrscr();
i=10;
f=4.567;
c='x';
iptr=&i;
fptr=&f;
cptr=&c;

piptr=&iptr;
pfptr=&fptr;
pcptr=&cptr;

printf("Address of i through iptr is %u\n",iptr);


printf("Address of f through fptr is %u\n",fptr);
printf("Address of c through cptr is %u\n",cptr);

printf("The value of i using *iptr is %d\n",*iptr);


printf("The value of f using *fptr is %f\n",*fptr);
printf("The value of c using *cptr is %c\n",*cptr);

printf("The address of iptr using piptr is %u\n",piptr);


printf("The address of fptr ysing pfptr is %u\n",pfptr);
printf("The address of cptr using pcptr is %u\n",pcptr);

printf("The value of i using **piptr is %d\n",**piptr);


printf("The value of f using **pfptr is %f\n",**pfptr);
printf("The value of c using **pcptr is %c\n",**pcptr);
return 0;
}

output:
Pointer Arithmatic:

We can perform few arithmatic operations on pointers.


We can either add a number to the pointer or subtract a number from the pointer.
We cannot add two pointers or we cannot subtract a pointer from another pointer which does not
gives any sense.

If we add some number to the pointer then it will add the bytes of number multiplied by the size
of datatype

If we subtract some number from the pointer then it will subtracts the bytes of number multiplied
by the size of datatype.

We can use the pointer arithmatic for accesing the array elements

int a[5]={10,20,30,40,50};
int *iptr;
iptr=&a[0]; or iptr=a;

iptr+0 => 100+(0*2) = 100


iptr+1 => 100+(1*2) = 102
iptr+2 => 100+(2*2) = 104
iptr+3 => 100+(3*2) = 106
iptr+4 => 100+(4*2) = 108

To access the elements of array using pointer


we can write as follows

*(iptr+0) => 100+(0*2) = *(100) -> 10


*(iptr+1) => 100+(1*2) = *(102) -> 20
*(iptr+2) => 100+(2*2) = *(104) -> 30
*(iptr+3) => 100+(3*2) = *(106) -> 40
*(iptr+4) => 100+(4*2) = *(108) -> 50

Write a program to create a pointer to array and access the addresses and elements of array using
pointer arithmatic technique, display addresses and elements of array.
ptr4.c

#include<stdio.h>
main()
{
int a[5]={10,20,30,40,50};
int *iptr,i;
iptr=&a[0];
clrscr();
printf("Address\t\tElement\n");
for(i=0;i<5;i++)
{
printf("%u\t\t%d\n",iptr+i,*(iptr+i));
}
return 0;
}
output:

Note:
We can also pass the array name to pointer or we can use the array name itsef as the pointer,
because the array name is a default pointer constant, which always points to the address of first
element of array.

Write a program to declare an array and use the array name as a pointer to display the addresses
and elements of array.
ptr5.c

#include<stdio.h>
main()
{
int a[5]={10,20,30,40,50};
int i;
clrscr();
printf("Address\t\tElement\n");
for(i=0;i<5;i++)
{
printf("%u\t\t%d\n",a+i,*(a+i));
}
return 0;
}
output:

Write a program to create a float array to access the addresses and elements using a pointer

ptr6.c

#include<stdio.h>
main()
{
float fa[5]={10.2,20.3,30.4,40.5,50.6};
int i;
float *fptr;
fptr=&fa[0];
clrscr();
printf("Address\t\tElement\n");
for(i=0;i<5;i++)
{
printf("%u\t\t%f\n",fptr+i,*(fptr+i));
}
return 0;
}

output:
Pointer to Structure:
We can also create a pointer to the structure
syntax:
struct structname
{
datatype var1;
datatype var2;
...
datatype varn;
}*sptr;

To access the elements of a structure using pointer we can use the member operator(->) with the
pointername.

syntax:
sptr->var1;
sptr->var2;
....
sptr->varn;

Write a program to create a structure and pointer to structure, access the structure members using
the pointer.

ptr7.c

#include<stdio.h>
void dummy(float *a)
{
float b=*a;
dummy(&b);
}
main()
{
struct employee
{
int eid;
char name[15];
float salary;
}*eptr,e;
clrscr();
eptr=&e;
printf("Enter eid,name and salary of employee\n");
scanf("%d",&eptr->eid);
scanf("%s",eptr->name);
scanf("%f",&eptr->salary);
printf("eid : %d\n",eptr->eid);
printf("name : %s\n",eptr->name);
printf("Salary : %f\n",eptr->salary);
return 0;
}
output:

Self Referential Structure:


If we declare a pointer variable of a structure within its definition then we call it as self
referential structure.

The use of pointers with structure is to create the linked lists.

Ex:
struct node
{
int data;
struct node *link;
};

void pointer:

We can creat a void pointer to assign any datatype variable's address.


We can define a void pointer using the keyword void.

syntax:
void *pointername;

ex:
void *vptr;
Note : When we try to access the value stored at the variable's address using void pointer we
need to apply the type conversion of corresponding variable's pointer type.

Write a program to demonstrate the void pointer.

ptr8.c

#include<stdio.h>
main()
{
void *vptr;
int i;
float f;
i=10;
f=20.5678;
clrscr();
vptr=&i;
printf("The address of i is %u\n",vptr);
printf("The value of i using vptr is %d\n",*(int *)vptr);
vptr=&f;
printf("The address of f is %u\n",vptr);
printf("The value of f using vptr is %f\n",*(float *)vptr);
return 0;
}
output:

You might also like