Professional Documents
Culture Documents
ARRAYS
1.Member operator(.)
2.Structure pointer operator(->)
Any member of a structure can be accessed
as:
structure_variable_name.member_name
#include<stdio.h>
void main()
{
struct book
{
char name;
float price;
int pages;
};
struct book b1,b2,b3;
printf(“Enter names,prices and no.of pages”);
scanf(“%c %f %d”, &b1.name, &b1.price, &b1.pages);
scanf(“%c %f %d”, &b2.name, &b2.price,
&b2.pages);
scanf(“%c %f %d”, &b3.name, &b3.price,
&b3.pages);
printf(“This is what you entered”);
printf(“%c %f %d”, b1.name, b1.price, b1.pages);
printf(“%c %f %d”, b2.name, b2.price, b2.pages);
printf(“%c %f %d”, b3.name, b3.price, b3.pages);
}
Define a structure data type called TimeStruct
containing 3 members called hour, minute and
second. Develop a program that would assign
values to the individual members and display
the time in the form 16:40:30
#include<stdio.h>
struct book
{
char book_name[30];
char author_name[30];
int price;
} b[20];
void main()
{
int n,i;
clrscr();
printf("Enter the no of books:");
scanf("%d",&n);
printf("BOOK DETAILS\n");
for(i=0;i<n;i++)
{
printf("Enter book name:");
scanf("%s",b[i].book_name);
printf("Enter author name:");
scanf("%s",b[i].author_name);
printf("Enter price:");
scanf("%d",&b[i].price);
}
printf("\nBook Name\tAuthor Name\tPrice\n");
printf("\n---------\t-----------\t-----\n");
for(i=0;i<n;i++)
{
printf("%s\t\t%s\t\t
%d\n",b[i].book_name,b[i].author_name,b[i].price);
}
getch();
}
Output
Enter the no of books:2
BOOK DETAILS
Enter book name:C
Enter author name:Balagurusamy
Enter price:300
Enter book name:.Net
Enter author name:Dravid
Enter price:500
Book Name Author Name Price
--------- ----------- -----
C Balagurusamy 300
.Net Dravid 500
Unions:
Major distinction between them in terms of storage.
In structures each member has its own storage location.
All the members of unions use the same location.
Union item
{
int m;
float x;
char c;
}code;
union char_and_ascii
{
char ch;
unsigned int ascii_val;
};
Memory uses by structure:
sizeof(char) + sizeof(unsigned int)
1 + 4 = 5 bytes.