CNotes 2
CNotes 2
CNotes 2
---------
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)
syntax:
datatype arrayname[size];
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
Ex:2
float avg[3]={99.4,99.3,80.0};
avg[0];
avg[1];
avg[2];
#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:
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:
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.
#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:
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.
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:
syntax:
char stringname[size];
ex:
char name[15];
Initialization of a string:
ex:
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:
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:
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:
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++;
}
output:
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.
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.
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);
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:
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:
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);
output:
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.
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.
syntax:
char stringname[size][size]
ex:
char names[5][15];
char names[5][15]={"Rajesh","Ramesh","Rahul","Ramu","Rohan"};
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:
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.
syntax:
struct structname
{
datatype member1;
datatype member2;
datatype member3;
.
.
datatype membern;
};
Ex;
struct student
{
int rno;
char name[20];
float avg;
};
syntax:
struct structname variablename;
ex:
struct student s1;
syntax:
variablename.member;
ex:
s1.rno;
s1.name;
s1.avg;
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.
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);
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
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:
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;
HW1:
Write a program to create an enumeration of weights and display the created weights
*******************************************************
---------------------- Pointers -----------------------
*******************************************************
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;
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;
return 0;
}
output:
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;
output:
Pointer Arithmatic:
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;
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:
Ex:
struct node
{
int data;
struct node *link;
};
void pointer:
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.
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: