SlideShare a Scribd company logo
1 of 18
© 2015UPESJuly 2015 Department. Of Civil Engineering
STRUCTURES
© 2015UPESJuly 2015 Department. Of Civil Engineering
INTRODUCTION
 A structure is same as that of records. It stores related information about an entity.
 Structure is basically a user defined data type that can store related information (even of different data
types) together.
 A structure is declared using the keyword struct followed by a structure name. All the variables of the
structures are declared within the structure. A structure type is defined by using the given syntax.
struct struct-name
{ data_type var-name;
data_type var-name;
...
};
struct student
{ int r_no;
char name[20];
char course[20];
float fees;
};
 The structure definition does not allocates any memory. It just gives a template that conveys to the C
compiler how the structure is laid out in memory and gives details of the member names. Memory is
allocated for the structure when we declare a variable of the structure. For ex, we can define a variable
of student by writing
struct student stud1;
© 2015UPESJuly 2015 Department. Of Civil Engineering
TYPEDEF DECLARATIONS
 When we precede a struct name with typedef keyword, then the struct becomes a new type. It is
used to make the construct shorter with more meaningful names for types already defined by C
or for types that you have declared. With a typedef declaration, becomes a synonym for the type.
 For example, writing
typedef struct student
{
int r_no;
char name[20];
char course[20];
float fees;
};
 Now that you have preceded the structure’s name with the keyword typedef, the student
becomes a new data type. Therefore, now you can straight away declare variables of this new
data type as you declare variables of type int, float, char, double, etc. to declare a variable of
structure student you will just write: student stud1;
© 2015UPESJuly 2015 Department. Of Civil Engineering
INITIALIZATION OF STRUCTURES
 Initializing a structure means assigning some constants to the members of the structure.
 When the user does not explicitly initializes the structure then C automatically does that. For int
and float members, the values are initialized to zero and char and string members are initialized
to the ‘0’ by default.
 The initializers are enclosed in braces and are separated by commas. Note that initializers match
their corresponding types in the structure definition.
 The general syntax to initialize a structure variable is given as follows.
struct struct_name
{ data_type member_name1;
data_type member_name2;
data_type member_name3;
.......................................
}struct_var = {constant1, constant2, constant 3,...};
OR
© 2015UPESJuly 2015 Department. Of Civil Engineering
struct struct_name
{ data_type member_name1;
data_type member_name2;
data_type member_name3;
.......................................
};struct struct_name struct_var = {constant1, constant2, ….};
struct student stud1 = {01, “Rahul”, “BCA”, 45000};
© 2015UPESJuly 2015 Department. Of Civil Engineering
ACCESSING THE MEMBERS OF A STRUCTURE
 Each member of a structure can be used just like a normal variable, but its name will be a
bit longer. A structure member variable is generally accessed using a ‘.’ (dot operator).
 The syntax of accessing a structure a member of a structure is:
struct_var.member_name
 For ex, to assign value to the individual data members of the structure variable Rahul, we
may write,
stud1.r_no = 01;
strcpy(stud1.name, “Rahul”);
stud1.course = “BCA”;
stud1.fees = 45000;
 We can assign a structure to another structure of the same type. For ex, if we have two structure
variables stud1 and stud2 of type struct student given as
struct student stud1 = {01, "Rahul", "BCA", 45000};
struct student stud2;
Then to assign one structure variable to another we will write, stud2 = stud1;
© 2015UPESJuly 2015 Department. Of Civil Engineering
Write a program using structures to read and
display the information about a student
#include<stdio.h>
int main()
{ struct student
{ int roll_no;
char name[80];
float fees;
char DOB[80];
};
struct student stud1;
printf(“n Enter the roll number : “);
scanf(“%d”, &stud1.roll_no);
printf(“n Enter the name : “);
scanf(“%s”, stud1.name);
printf(“n Enter the fees : “);
scanf(“%f”, &stud1.fees);
printf(“n Enter the DOB : “);
scanf(“%s”, stud1.DOB);
printf(“n ********STUDENT’S DETAILS *******”);
printf(“n ROLL No. = %d”, stud1.roll_no);
printf(“n NAME. = %s”, stud1.name);
printf(“n ROLL No. = %f”, stud1.fees);
printf(“n ROLL No. = %s”, stud1.DOB);
}
© 2015UPESJuly 2015 Department. Of Civil Engineering
Write a program to read and display information
of a student using structure within a structure
 #include<stdio.h>
 int main()
 { struct DOB
 {
 int day;
 int month;
 int year;
 };
 struct student
 { int roll_no;
 char name[100];
 float fees;
 struct DOB date;
 };
 struct student stud1;
 printf(“n Enter the roll number : “);
 scanf(“%d”, &stud1.roll_no);
 printf(“n Enter the name : “);
 scanf(“%s”, stud1.name);
 printf(“n Enter the fees : “);
 scanf(“%f”, &stud1.fees);
 printf(“n Enter the DOB : “);
 scanf(“%d %d %d”, &stud1.date.day, &stud1.date.month, &stud1.date.year);
 printf(“n ********STUDENT’S DETAILS *******”);
 printf(“n ROLL No. = %d”, stud1.roll_no);
 printf(“n NAME. = %s”, stud1.name);
 printf(“n FEES. = %f”, stud1.fees);
 printf(“n DOB = %d - %d - %d”, stud1.date.day, stud1.date.month, stud1.date.year);
 }
© 2015UPESJuly 2015 Department. Of Civil Engineering
ARRAYS OF STRUCTURES
 The general syntax for declaring an array of structure can be given as,
struct struct_name struct_var[index];
struct student stud[30];
Now, to assign values to the ith student of the class, we will write,
stud[i].r_no = 09;
stud[i].name = “RASHI”;
stud[i].course = “MCA”;
stud[i].fees = 60000;
© 2015UPESJuly 2015 Department. Of Civil Engineering
Write a program to read and display information
of all the students in the class.
 #include<stdio.h>
 int main()
 { struct student
 {
 int roll_no;
 char name[80];
 float fees;
 char DOB[80];
 };
 struct student stud[50];
 int n, i;
 printf(“n Enter the number of students : “);
 scanf(“%d”, &n);
 for(i=0;i<n;i++)
 { printf(“n Enter the roll number : “);
 scanf(“%d”, &stud[i].roll_no);
 printf(“n Enter the name : “);
 scanf(“%s”, stud[i].name);
 printf(“n Enter the fees : “);
 scanf(“%f”, stud[i].fees);
 printf(“n Enter the DOB : “);
 scanf(“%s”, stud[i].DOB);
 }
 for(i=0;i<n;i++)
 { printf(“n ********DETAILS OF %dth STUDENT*******”, i+1);
 printf(“n ROLL No. = %d”, stud[i].roll_no);
 printf(“n NAME. = %s”, stud[i].name);
 printf(“n ROLL No. = %f”, stud[i].fees);
 printf(“n ROLL No. = %s”, stud[i].DOB);
 }
 }
© 2015UPESJuly 2015 Department. Of Civil Engineering
Accessing Structure Members using
Pointers
 C structure can be accessed in 2 ways in a C program. They are,
 Using normal structure variable
 Using pointer variable
Dot(.) operator is used to access the data using normal structure
variable and arrow (->) is used to access the data using pointer
variable.
© 2015UPESJuly 2015 Department. Of Civil Engineering
#include <stdio.h>
#include <string.h>
struct student { int id;
char name[30];
float percentage;};
int main() {int i;
struct student record1 = {1, "Raju", 90.5};
struct student *ptr;
ptr = &record1;
printf("Records of STUDENT1: n");
printf(" Id is: %d n", ptr->id);
printf(" Name is: %s n", ptr->name);
printf(" Percentage is: %f nn", ptr->percentage);
return 0;}
© 2015UPESJuly 2015 Department. Of Civil Engineering
UNION
 Like structure, a union is a collection of variables of different data types. The only difference
between a structure and a union is that in case of unions, you can only store information in one
field at any one time.
 To better understand union, think of it as a chunk of memory that is used to store variables of
different types. When a new value is assigned to a field, the existing data is replaced with the
new data.
 Thus unions are used to save memory. They are useful for applications that involve multiple
members, where values need not be assigned to all the members at any one time.
DECLARING A UNION The syntax for declaring a union is same as that of declaring a structure.
union union-name
{ data_type var-name;
data_type var-name; ..};
Again, the typedef keyword can be used to simplify the declaration of union variables.
The most important thing to remember about a union is that the size of an union is the size of its
largest field. This is because a sufficient number of bytes must be reserved to store the largest
sized field.
© 2015UPESJuly 2015 Department. Of Civil Engineering
ACCESSING A MEMBER OF A UNION
 A member of a union can be accessed using the same syntax as that of a structure. To access
the fields of a union, use the dot operator(.). That is the union variable name followed by the dot
operator followed by the member name.
INITIALIZING UNIONS
 It is an error to initialize any other union member except the first member
 A striking difference between a structure and a union is that in case of a union, the fields share
the same memory space, so fresh data replaces any existing data. Look at the code given below
and observe the difference between a structure and union when their fields are to be initialized.
© 2015UPESJuly 2015 Department. Of Civil Engineering
#include<stdio.h>
typedef struct POINT1
{ int x, y;
};
typedef union POINT2
{
int x;
int y;
};
main()
{ POINT1 P1 = {2,3};
// POINT2 P2 ={4,5}; Illegeal with union
POINT2 P2;
P2. x = 4;
P2.y = 5;
printf("n The co-ordinates of P1 are %d and %d", P1.x, P1.y);
printf("n The co-ordinates of P2 are %d and %d", P2.x, P2.y);
return 0;
}
OUTPUT
The co-ordinates of P1 are 2 and 3
The co-ordinates of P2 are 5 and 5
© 2015UPESJuly 2015 Department. Of Civil Engineering
UNIONS INSIDE STRUCTURES
 union can be very useful when declared inside a structure. Consider an example in which you
want a field of a structure to contain a string or an integer, depending on what the user specifies.
The following code illustrates such a scenario.
struct student
{ union
{ char name[20];
int roll_no;
};
int marks;
};
main()
{ struct student stud;
char choice;
printf("n You can enter the name or roll number of the student");
printf("n Do you want to enter the name? (Yes or No) : ");
gets(choice);
if(choice=='y' || choice=='Y')
{ printf("n Enter the name : ");
gets(stud.name);
}
© 2015UPESJuly 2015 Department. Of Civil Engineering
 else
 { printf("n Enter the roll number : ");
scanf("%d", &stud.roll_no);
 }
 printf("n Enter the marks : ");
 scanf("%d", &stud.marks);
 if(choice=='y' || choice=='Y')
printf("n Name : %s ", stud.name);
 else
printf("n Roll Number : %d ", stud.roll_no);
 printf("n Marks : %d", stud.marks);
 }
© 2015UPESJuly 2015 Department. Of Civil Engineering
Compare Structure and Union

More Related Content

What's hot (19)

C Structures And Unions
C  Structures And  UnionsC  Structures And  Unions
C Structures And Unions
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
 
structure and union
structure and unionstructure and union
structure and union
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
PPT
PPTPPT
PPT
 
Module 4- Arrays and Strings
Module 4- Arrays and StringsModule 4- Arrays and Strings
Module 4- Arrays and Strings
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structures and Unions
Structures and UnionsStructures and Unions
Structures and Unions
 
Structure in C
Structure in CStructure in C
Structure in C
 
CPU : Structures And Unions
CPU : Structures And UnionsCPU : Structures And Unions
CPU : Structures And Unions
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
 
Arrays
ArraysArrays
Arrays
 
Structures
StructuresStructures
Structures
 
C structure and union
C structure and unionC structure and union
C structure and union
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
 
Module 1:Introduction
Module 1:IntroductionModule 1:Introduction
Module 1:Introduction
 

Similar to Civil Engineering Structures Notes

358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7sumitbardhan
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structuresKrishna Nanda
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfsudhakargeruganti
 
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA REC UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA RERajeshkumar Reddy
 
Structure In C
Structure In CStructure In C
Structure In Cyndaravind
 
Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9patcha535
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing conceptskavitham66441
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referentialbabuk110
 
STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING Gurwinderkaur45
 
Structures and Pointers
Structures and PointersStructures and Pointers
Structures and PointersPrabu U
 
Structures
StructuresStructures
Structuresselvapon
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and StructuresGem WeBlog
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 

Similar to Civil Engineering Structures Notes (20)

structure1.pdf
structure1.pdfstructure1.pdf
structure1.pdf
 
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
 
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA REC UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
 
Structure In C
Structure In CStructure In C
Structure In C
 
Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9
 
Ocs752 unit 5
Ocs752   unit 5Ocs752   unit 5
Ocs752 unit 5
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referential
 
STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING
 
CP Handout#10
CP Handout#10CP Handout#10
CP Handout#10
 
Structures
StructuresStructures
Structures
 
Structures and Pointers
Structures and PointersStructures and Pointers
Structures and Pointers
 
Structures
StructuresStructures
Structures
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
Unit-V.pptx
Unit-V.pptxUnit-V.pptx
Unit-V.pptx
 
Structures
StructuresStructures
Structures
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 

More from Mitali Chugh (20)

Loc and function point
Loc and function pointLoc and function point
Loc and function point
 
Unit1
Unit1Unit1
Unit1
 
Unit 2 ppt
Unit 2 pptUnit 2 ppt
Unit 2 ppt
 
Module 4
Module 4Module 4
Module 4
 
Blogs
BlogsBlogs
Blogs
 
Module 5 and 6
Module 5 and 6Module 5 and 6
Module 5 and 6
 
Types of computer
Types of computerTypes of computer
Types of computer
 
Module 2 os
Module 2 osModule 2 os
Module 2 os
 
Os ppt
Os pptOs ppt
Os ppt
 
Upes ppt template
Upes ppt templateUpes ppt template
Upes ppt template
 
Functions
FunctionsFunctions
Functions
 
Functions
FunctionsFunctions
Functions
 
Strings
StringsStrings
Strings
 
Arrays
ArraysArrays
Arrays
 
Control flow stataements
Control flow stataementsControl flow stataements
Control flow stataements
 
Unit 2 l1
Unit 2 l1Unit 2 l1
Unit 2 l1
 
Unit1
Unit1Unit1
Unit1
 
How to compile and run a c program on ubuntu linux
How to compile and run a c program on ubuntu linuxHow to compile and run a c program on ubuntu linux
How to compile and run a c program on ubuntu linux
 
Blogs
BlogsBlogs
Blogs
 
Module 4
Module 4Module 4
Module 4
 

Recently uploaded

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...Call girls in Ahmedabad High profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 

Recently uploaded (20)

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 

Civil Engineering Structures Notes

  • 1. © 2015UPESJuly 2015 Department. Of Civil Engineering STRUCTURES
  • 2. © 2015UPESJuly 2015 Department. Of Civil Engineering INTRODUCTION  A structure is same as that of records. It stores related information about an entity.  Structure is basically a user defined data type that can store related information (even of different data types) together.  A structure is declared using the keyword struct followed by a structure name. All the variables of the structures are declared within the structure. A structure type is defined by using the given syntax. struct struct-name { data_type var-name; data_type var-name; ... }; struct student { int r_no; char name[20]; char course[20]; float fees; };  The structure definition does not allocates any memory. It just gives a template that conveys to the C compiler how the structure is laid out in memory and gives details of the member names. Memory is allocated for the structure when we declare a variable of the structure. For ex, we can define a variable of student by writing struct student stud1;
  • 3. © 2015UPESJuly 2015 Department. Of Civil Engineering TYPEDEF DECLARATIONS  When we precede a struct name with typedef keyword, then the struct becomes a new type. It is used to make the construct shorter with more meaningful names for types already defined by C or for types that you have declared. With a typedef declaration, becomes a synonym for the type.  For example, writing typedef struct student { int r_no; char name[20]; char course[20]; float fees; };  Now that you have preceded the structure’s name with the keyword typedef, the student becomes a new data type. Therefore, now you can straight away declare variables of this new data type as you declare variables of type int, float, char, double, etc. to declare a variable of structure student you will just write: student stud1;
  • 4. © 2015UPESJuly 2015 Department. Of Civil Engineering INITIALIZATION OF STRUCTURES  Initializing a structure means assigning some constants to the members of the structure.  When the user does not explicitly initializes the structure then C automatically does that. For int and float members, the values are initialized to zero and char and string members are initialized to the ‘0’ by default.  The initializers are enclosed in braces and are separated by commas. Note that initializers match their corresponding types in the structure definition.  The general syntax to initialize a structure variable is given as follows. struct struct_name { data_type member_name1; data_type member_name2; data_type member_name3; ....................................... }struct_var = {constant1, constant2, constant 3,...}; OR
  • 5. © 2015UPESJuly 2015 Department. Of Civil Engineering struct struct_name { data_type member_name1; data_type member_name2; data_type member_name3; ....................................... };struct struct_name struct_var = {constant1, constant2, ….}; struct student stud1 = {01, “Rahul”, “BCA”, 45000};
  • 6. © 2015UPESJuly 2015 Department. Of Civil Engineering ACCESSING THE MEMBERS OF A STRUCTURE  Each member of a structure can be used just like a normal variable, but its name will be a bit longer. A structure member variable is generally accessed using a ‘.’ (dot operator).  The syntax of accessing a structure a member of a structure is: struct_var.member_name  For ex, to assign value to the individual data members of the structure variable Rahul, we may write, stud1.r_no = 01; strcpy(stud1.name, “Rahul”); stud1.course = “BCA”; stud1.fees = 45000;  We can assign a structure to another structure of the same type. For ex, if we have two structure variables stud1 and stud2 of type struct student given as struct student stud1 = {01, "Rahul", "BCA", 45000}; struct student stud2; Then to assign one structure variable to another we will write, stud2 = stud1;
  • 7. © 2015UPESJuly 2015 Department. Of Civil Engineering Write a program using structures to read and display the information about a student #include<stdio.h> int main() { struct student { int roll_no; char name[80]; float fees; char DOB[80]; }; struct student stud1; printf(“n Enter the roll number : “); scanf(“%d”, &stud1.roll_no); printf(“n Enter the name : “); scanf(“%s”, stud1.name); printf(“n Enter the fees : “); scanf(“%f”, &stud1.fees); printf(“n Enter the DOB : “); scanf(“%s”, stud1.DOB); printf(“n ********STUDENT’S DETAILS *******”); printf(“n ROLL No. = %d”, stud1.roll_no); printf(“n NAME. = %s”, stud1.name); printf(“n ROLL No. = %f”, stud1.fees); printf(“n ROLL No. = %s”, stud1.DOB); }
  • 8. © 2015UPESJuly 2015 Department. Of Civil Engineering Write a program to read and display information of a student using structure within a structure  #include<stdio.h>  int main()  { struct DOB  {  int day;  int month;  int year;  };  struct student  { int roll_no;  char name[100];  float fees;  struct DOB date;  };  struct student stud1;  printf(“n Enter the roll number : “);  scanf(“%d”, &stud1.roll_no);  printf(“n Enter the name : “);  scanf(“%s”, stud1.name);  printf(“n Enter the fees : “);  scanf(“%f”, &stud1.fees);  printf(“n Enter the DOB : “);  scanf(“%d %d %d”, &stud1.date.day, &stud1.date.month, &stud1.date.year);  printf(“n ********STUDENT’S DETAILS *******”);  printf(“n ROLL No. = %d”, stud1.roll_no);  printf(“n NAME. = %s”, stud1.name);  printf(“n FEES. = %f”, stud1.fees);  printf(“n DOB = %d - %d - %d”, stud1.date.day, stud1.date.month, stud1.date.year);  }
  • 9. © 2015UPESJuly 2015 Department. Of Civil Engineering ARRAYS OF STRUCTURES  The general syntax for declaring an array of structure can be given as, struct struct_name struct_var[index]; struct student stud[30]; Now, to assign values to the ith student of the class, we will write, stud[i].r_no = 09; stud[i].name = “RASHI”; stud[i].course = “MCA”; stud[i].fees = 60000;
  • 10. © 2015UPESJuly 2015 Department. Of Civil Engineering Write a program to read and display information of all the students in the class.  #include<stdio.h>  int main()  { struct student  {  int roll_no;  char name[80];  float fees;  char DOB[80];  };  struct student stud[50];  int n, i;  printf(“n Enter the number of students : “);  scanf(“%d”, &n);  for(i=0;i<n;i++)  { printf(“n Enter the roll number : “);  scanf(“%d”, &stud[i].roll_no);  printf(“n Enter the name : “);  scanf(“%s”, stud[i].name);  printf(“n Enter the fees : “);  scanf(“%f”, stud[i].fees);  printf(“n Enter the DOB : “);  scanf(“%s”, stud[i].DOB);  }  for(i=0;i<n;i++)  { printf(“n ********DETAILS OF %dth STUDENT*******”, i+1);  printf(“n ROLL No. = %d”, stud[i].roll_no);  printf(“n NAME. = %s”, stud[i].name);  printf(“n ROLL No. = %f”, stud[i].fees);  printf(“n ROLL No. = %s”, stud[i].DOB);  }  }
  • 11. © 2015UPESJuly 2015 Department. Of Civil Engineering Accessing Structure Members using Pointers  C structure can be accessed in 2 ways in a C program. They are,  Using normal structure variable  Using pointer variable Dot(.) operator is used to access the data using normal structure variable and arrow (->) is used to access the data using pointer variable.
  • 12. © 2015UPESJuly 2015 Department. Of Civil Engineering #include <stdio.h> #include <string.h> struct student { int id; char name[30]; float percentage;}; int main() {int i; struct student record1 = {1, "Raju", 90.5}; struct student *ptr; ptr = &record1; printf("Records of STUDENT1: n"); printf(" Id is: %d n", ptr->id); printf(" Name is: %s n", ptr->name); printf(" Percentage is: %f nn", ptr->percentage); return 0;}
  • 13. © 2015UPESJuly 2015 Department. Of Civil Engineering UNION  Like structure, a union is a collection of variables of different data types. The only difference between a structure and a union is that in case of unions, you can only store information in one field at any one time.  To better understand union, think of it as a chunk of memory that is used to store variables of different types. When a new value is assigned to a field, the existing data is replaced with the new data.  Thus unions are used to save memory. They are useful for applications that involve multiple members, where values need not be assigned to all the members at any one time. DECLARING A UNION The syntax for declaring a union is same as that of declaring a structure. union union-name { data_type var-name; data_type var-name; ..}; Again, the typedef keyword can be used to simplify the declaration of union variables. The most important thing to remember about a union is that the size of an union is the size of its largest field. This is because a sufficient number of bytes must be reserved to store the largest sized field.
  • 14. © 2015UPESJuly 2015 Department. Of Civil Engineering ACCESSING A MEMBER OF A UNION  A member of a union can be accessed using the same syntax as that of a structure. To access the fields of a union, use the dot operator(.). That is the union variable name followed by the dot operator followed by the member name. INITIALIZING UNIONS  It is an error to initialize any other union member except the first member  A striking difference between a structure and a union is that in case of a union, the fields share the same memory space, so fresh data replaces any existing data. Look at the code given below and observe the difference between a structure and union when their fields are to be initialized.
  • 15. © 2015UPESJuly 2015 Department. Of Civil Engineering #include<stdio.h> typedef struct POINT1 { int x, y; }; typedef union POINT2 { int x; int y; }; main() { POINT1 P1 = {2,3}; // POINT2 P2 ={4,5}; Illegeal with union POINT2 P2; P2. x = 4; P2.y = 5; printf("n The co-ordinates of P1 are %d and %d", P1.x, P1.y); printf("n The co-ordinates of P2 are %d and %d", P2.x, P2.y); return 0; } OUTPUT The co-ordinates of P1 are 2 and 3 The co-ordinates of P2 are 5 and 5
  • 16. © 2015UPESJuly 2015 Department. Of Civil Engineering UNIONS INSIDE STRUCTURES  union can be very useful when declared inside a structure. Consider an example in which you want a field of a structure to contain a string or an integer, depending on what the user specifies. The following code illustrates such a scenario. struct student { union { char name[20]; int roll_no; }; int marks; }; main() { struct student stud; char choice; printf("n You can enter the name or roll number of the student"); printf("n Do you want to enter the name? (Yes or No) : "); gets(choice); if(choice=='y' || choice=='Y') { printf("n Enter the name : "); gets(stud.name); }
  • 17. © 2015UPESJuly 2015 Department. Of Civil Engineering  else  { printf("n Enter the roll number : "); scanf("%d", &stud.roll_no);  }  printf("n Enter the marks : ");  scanf("%d", &stud.marks);  if(choice=='y' || choice=='Y') printf("n Name : %s ", stud.name);  else printf("n Roll Number : %d ", stud.roll_no);  printf("n Marks : %d", stud.marks);  }
  • 18. © 2015UPESJuly 2015 Department. Of Civil Engineering Compare Structure and Union