You are on page 1of 26

STRUCTURES

ARRAYS

•Arrays are a data structure which hold


multiple variables of the same data type.
•Collection of similar data types , called array.
•Store more than one value at a time in a
single variable
• Declaration of Array:
Syntax:
datatype arrayname[size];
Examples:
int names[4];
float number[10];
char name[15];
• The array names contains four elements.
There are
• names[0]
• names[1]
• names[2]
• names[3]
Arrays have the following syntax, using
square brackets to access each indexed
value (called an element).
• x[i]
• We could access the first element as
follows:
int scores[10];

• To process all the elements in scores, a loop


similar to the
following code is used :

for ( i = 0; i <9, i++ )


scanf(“%d”,&scores[i] );
• A structure contains a number of data types grouped
together.
• These data types may or may not be of the same
type
• Collection of different data types.
Syntax:
struct struct-name
{
type field-name;
type field-name; ...
};
Arrays allow to define type of variables that
can hold several data items of the same kind.

structure is another user defined data type


available in C that allows to combine data
items of different kinds.
• Structures are used to represent a record.
Suppose you want to keep track of your
books in a library. You might want to track
the following attributes about each book −
• Title
• Author
• Subject
• Book ID
• Declaration of a structure
• e.g.
struct student_type
{
char name[20];
int ID;
}

Accessing of structure elements


Structure variable declaration
struct person
{
char name[50];
int cit_no;
float salary;
};

Inside main function:


struct person p1, p2, p3;
Another way to declare structure variable
struct person
{
char name[50];
int cit_no;
float salary;
}p1 ,p2 ,p3;
Accessing members of a structure
There are two types of operators used for
accessing members of a structure.

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.

But, in case of a union, the size is equivalent to that of the largest


member type in union.
So, in this case, the largest type is ‘unsigned int’ and hence the size of
union becomes ‘4’.
• you want to use only one of the members at a
time.

• So in that case, using a union is a wise option


rather than using a structure.

• This will save you memory.


#include <stdio.h>
union char_and_ascii
{
char ch;
unsigned short ascii_val;
};
int main (void)
{
union char_and_ascii obj;
obj.ascii_val = 0;
obj.ch = 'A';
printf("\n character = [%c], ascii_value = [%u]\n",
obj.ch, obj.ascii_val);
return 0; }
Output:
character = [A], ascii_value = [65]

You might also like