0% found this document useful (0 votes)
13 views

Structures

The document discusses structures in C programming. Structures allow grouping of different data types together under one name to represent a record. The document demonstrates how to: 1. Declare a structure type with member elements like name, price, pages. 2. Declare structure variables of the given type to store data for multiple objects. 3. Access individual members of a structure using the dot operator. 4. Structure members are stored sequentially in memory with addresses increasing. 5. Arrays of structures can be used to store information about multiple entities.

Uploaded by

gsc5741
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Structures

The document discusses structures in C programming. Structures allow grouping of different data types together under one name to represent a record. The document demonstrates how to: 1. Declare a structure type with member elements like name, price, pages. 2. Declare structure variables of the given type to store data for multiple objects. 3. Access individual members of a structure using the dot operator. 4. Structure members are stored sequentially in memory with addresses increasing. 5. Arrays of structures can be used to store information about multiple entities.

Uploaded by

gsc5741
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Structures

Why use Structures?


We have seen earlier how ordinary variables can hold one piece of information and
how arrays can hold a number of pieces of information of the same data type. These
two data types can handle a great variety of situations. But quite often we deal with
entities that are collection of dissimilar data types.
For example, suppose you want to store data about a book. You might want to store
its name (a string), its price (a float) and number of pages in it (an int). If data about
say 3 such books is to be stored, then we can follow two approaches:
(a) Construct individual arrays, one for storing names, another for storing prices and
still another for storing number of pages.
(b) Use a structure variable.
Example:
# include <stdio.h> output:--
int main( )
{ Enter names, prices and no. of
char name[ 3 ] ; pages of 3 books
float price[ 3 ] ; A 100.00 354
int pages[ 3 ], i ; C 256.50 682
printf ( "Enter names, prices and no. of pages of 3 books\n" ) ; F 233.70 512
for ( i = 0 ; i <= 2 ; i++ ) And this is what you entered
scanf ( "%c %f %d", &name[ i ], &price[ i ], &pages[ i ] ) ; A 100.000000 354
printf ( "\nAnd this is what you entered\n" ) ; C 256.500000 682
for ( i = 0 ; i <= 2 ; i++ ) F 233.700000 512
printf ( "%c %f %d\n", name[ i ], price[ i ], pages[ i ] ) ;
return 0 ;
}
Explanation
The program becomes more difficult to handle as the number of items relating to
the book goes on increasing.
For example, we would be required to use a number of arrays, if we also decide to
store name of the publisher, date of purchase of book, etc.
To solve this problem, C provides a special data type—the structure.
A structure contains a number of data types grouped together. These data types
may or may not be of the same type. The following example illustrates the use of
this data type:
Example using structure
# include <stdio.h> printf ( "Enter names, prices & no. of pages of 3 books\n" ) ;
int main( ) 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 ) ;
struct book
{ printf ( "And this is what you entered\n" ) ;
printf ( "%c %f %d\n", b1.name, b1.price, b1.pages ) ;
char name ;
printf ( "%c %f %d\n", b2.name, b2.price, b2.pages ) ;
float price ; printf ( "%c %f %d\n", b3.name, b3.price, b3.pages ) ;
int pages ; return 0 ;
}; }
struct book b1, b2, b3 ;
Output:-
Enter names, prices and no. of pages of 3 books
A 100.00 354
C 256.50 682
F 233.70 512
And this is what you entered
A 100.000000 354
C 256.500000 682
F 233.700000 512

This program demonstrates two fundamental aspects of structures:


(a) Declaration of a structure
(b) Accessing of structure elements
Declaring a Structure

In our example program, the following statement declares the structure type:
struct book
{
char name ;
float price ;
int pages ;
};

This statement defines a new data type called struct book. Each variable of this data type
will consist of a character variable called name, a float variable called price and an
integer variable called pages.
structure declaration statement
struct <structure name> Once the new structure data type has been defined, one or
{ more variables can be declared to be of that type.

structure element 1 ; For example, the variables b1, b2, b3 can be declared to be
structure element 2 ; of the type struct book, as,
structure element 3 ;
struct book b1, b2, b3 ;
......
...... This statement sets aside space in memory. It makes
}; available space to hold all the elements in the structure—in
this case, 7 bytes—one for name, four for price and two for
pages.
These bytes are always in adjacent memory locations.
structure declaration statement
we can combine the declaration of the structure type and the structure variables in
one statement.

struct book struct book struct


{ { {
char name ; char name ; char name ;
float price ; or float price ; or float price ;
int pages ; int pages ; int pages ;
}; } b1, b2, b3 ; } b1, b2, b3 ;
struct book b1, b2, b3 ;
Initialization of structure

Like primary variables and arrays, structure variables can also be initialized where
they are declared.

struct book
{
char name[ 10 ] ;
float price ;
int pages ;
};
struct book b1 = { "Basic", 130.00, 550 } ;
struct book b2 = { "Physics", 150.80, 800 } ;
struct book b3 = { 0 } ;
Points in declaring a structure type:

(a) The closing brace ( } ) in the structure type declaration must be followed by a semicolon ( ; ).

(b) It is important to understand that a structure type declaration does not tell the compiler to
reserve any space in memory. All a structure declaration does is, it defines the ‘form’ of the
structure.

(c) Usually structure type declaration appears at the top of the source code file, before any
variables or functions are defined. In very large programs they are usually put in a separate
header file, and the file is included (using the preprocessor directive #include) in whichever
program we want to use this structure type.

(d) If a structure variable is initiated to a value { 0 }, then all its elements are set to value 0, as in
b3 above. This is a handy way of initializing structure variables. In absence of this, we would
have been required to initialize each individual element to a value 0.
Accessing Structure Elements
We can access individual elements of an array using a subscript. Structures use a
different scheme. They use a dot (.) operator.
Example:
b1.pages
Similarly, to refer to price, we would use,
b1.price
How Structure Elements are Stored?
/* Memory map of structure elements */
Output:-
# include <stdio.h>
Address of name = 65518
int main( )
{ Address of price = 65519
struct book Address of pages = 65523
{
char name ;
float price ;
int pages ;
};
struct book b1 = { 'B', 130.00, 550 } ;
printf ( "Address of name = %u\n", &b1.name ) ;
printf ( "Address of price = %u\n", &b1.price ) ;
printf ( "Address of pages = %u\n", &b1.pages ) ;
return 0 ;
}
Array of Structures
An array of structres in C can be defined as the collection of multiple structures variables where each variable contains
information about different entities. The array of structures in C are used to store information about multiple entities of
different data types. The array of structures is also known as the collection of structures.
Example:-
#include<stdio.h> printf("\nEnter Rollno:");
#include <string.h> scanf("%d",&st[i].rollno);
struct student{
printf("\nEnter Name:");
int rollno;
scanf("%s",&st[i].name);
char name[10];
}
};
int main(){ printf("\nStudent Information List:");

int i; for(i=0;i<5;i++){

struct student st[5]; printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);


printf("Enter Records of 5 students"); }
for(i=0;i<5;i++)
return 0;
{
}
Write a program to print this
123

246

369

You might also like