Professional Documents
Culture Documents
Unit - 5 - GVK
Unit - 5 - GVK
Unit - 5 - GVK
Engineering &Technology
Department of ECE
SUBJECT: C PROGRAMMING
Subject Code:
Vijaya Kumar G
Assistant Professor, ECE
• A struct is also a collection of data items, except with a struct the data
items can have different data types, and the individual fields within the
struct are accessed by name instead of an integer index.
• A user-defined DATA TYPE which holds more than one elements of different
data types.
#include<stdio.h>
struct Test{
int a, b;
};
void main() {
struct Test r1 ;
r1.a = 20;
r1.b = 50;
printf (“a = %d, b = %d", r1.a, r1.b);
}
Department of ECE, VNRVJIET, Hyderabad March 29, 2022 14
Initialization
Structure members can also be initialized using curly braces ‘{}’
struct Test{
int a, b;
};
void main() {
struct Test r1 = {5, 6};
//struct Test r1 = {.b =6, .a = 5};
printf (“a = %d, b = %d", r1.a, r1.b);
}
Department of ECE, VNRVJIET, Hyderabad March 29, 2022 15
Initialization and accessing of structures
Rules for Initialization of structure
i) We cannot initialize individual members inside structure template.
ii) The order of values enclosed in braces must match with order of
members in structure definition.
iii) partial initialization is permitted ,we can initialize only the 1st few
and leave the remaining blank.
NOTE: non-initialized members should be only at the end of the list
iv)The non-initialized members will be assigned with default values as follows
0(zero) for integers and floating point number ‘\0’ for characters and strings
We can initialize the structure at compile time or at runtime
Initialization can be done in the following ways
1.struct book
{
int pages;
char author[30];
float price;
} b = {100, “balu”, 325.75};
2.struct book
{
int pages;
char author[30];
float price;
};
struct book b = {100, “balu”, 325.75};
3.using member operator
struct book
{
int pages;
char author[30];
float price;
};
struct book b;
b. pages = 100;
strcpy (b.author, “balu”);
b.price = 325.75;
4.using scanf ( )
struct book
{
int pages;
char author[30];
float price;
};
struct book b;
scanf (“%d”, &b.pages);
scanf (“%s”, b.author);
scanf (“%f”, &b. price);
In the above examples first three ways are compile time initialization and the fourth way
i.e using scanf is runtime initialization.
Complex Structures:
i) Array of structures
ii) Arrays with in Structure
iii) Nested structure
iv) Pointer to structure
(i) Each member of a student can be passed individually as function arguments but
this method becomes difficult when the structure contains more number of elements.
(ii) A copy of entire structure can be passed from calling function to the called
function. Here, the changes made in called function do not reflect in the calling function.
(iii) The address location of the structure can be passed to the function. Here the
changes made in the called function are reflected in the calling function.
int main()
{
struct Test r1 = {10, 20};
struct student
{
int id;
char name[20];
float percentage;
};
struct student record; // Global declaration of structure
void structure_demo();
int main()
{
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
structure_demo();
return 0;
}
void structure_demo()
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}
Example:
UC a, b;
int main( ) {
Book_details book;
strcpy( book.title, "C Programming");
strcpy( book.author, "Nuha Ali");
strcpy( book.subject, "C Programming Tutorial");
book.book_id = 6495407;
Therefore, to avoid wastage of memory, we mention the exact bit size. The
name and size of bit fields are defined using a structure.
int main( ) {
printf( "Memory size occupied by status1 : %d\n", sizeof(status1));
printf( "Memory size occupied by status2 : %d\n", sizeof(status2));
return 0;
}
▪ If you will use up to 32 variables each one with a width of 1 bit, then
also the status structure will use 4 bytes. However as soon as you
have 33 variables, it will allocate the next slot of the memory and it will
start using 8 bytes.
➢ only one member of the union can occupy the memory at once
➢ size of the union in any instance is equal to the size of its largest
element