UrbanPro
true

Learn C Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Structures in C

Venkateswararao S.
19/01/2017 0 0

A structure is a collection of one or more data members possibly of different data types, grouped together under a single name for convenient handling.

Defining a Structure:

          In general terms, the composition of a structure may be defined as:               

                             struct           tag{

                                                          member 1;

                                                          member 2;

                                                               ---          

                                                          member m;

                                                          };

          In the above declaration, struct is a required keyword, tag is a name that identifies of this type, and member1, member2, --- member m are individual member declarations.

          The individual members can be ordinary variables, pointers, arrays, or other structures. The member names within a particular structure must be distinct from one another, through a member name can be the same as the name of a variable defined outside of the structure.

          Once the composition of the structure has been defined, individual structure – type variables can be declared as follows.

                   storage – class struct tag variable 1, variable 2, --- , variable n;

          Where storage – class is an optional storage class specifier, struct is a required keyword, tag is the name that appeared in the structure type declaration, and variable 1, variable 2, ---, variable n are structure variables of type tag.

Ex:-   struct          account{

                                                int     acct_no;

                                                char   acct_type;

                                                char   name[80];

                                                float   balance;

                                                };

 

          We can declare the structure variables oldcustomer and newcustomer as follows:

 Ex:-  struct          account       oldcustomer, newcustomer;

          Oldcustomer and newcustomer are variables of type account.

          To combine the declaration of the structure composition with that of the structure variables, as shown below. 

          storage – class      struct           tag{

                                                                   member 1;

                                                                   member 2;

                                                                         ---          

                                                                   member m;                                                                                                            }variable 1, variable 2, ---, variable n;

          The tag is optional in this situation.

          The members of a structures variable can be assigned initial values as same as the elements of an array. The initial values must appear in the order in which they will be assigned to their corresponding structure members, enclosed in braces and separated by commas. The general from is 

          storage – class struct      tag     variable={ value 1, value 2, ---, value m}

          Where value1 reference to the value of the first member, value2 refers to the value of the second member, and so on.

          A structure may be defined as a member of another structure. In such situations, the declaration of the embedded structure must appear before the declaration of the outer structure.

          struct    date{

                                      int     month;

                                      int     day;

                                      int     year;

                                      };

          struct           account{

                                                int     acct_no;

                                                char   acct_type;

                                                char   name[80];

                                                float   balance;

                                                struct date  lastpayment;

                                                }oldcustomer, newcustomer;

          The structure account now contains another structure, date, as one of its members. Not that the declaration of date precedes the declaration of account.

          We can define an array of structures, that is, an array in which each element is a structure.   

Ex:-   struct    date{

                                      int     month;

                                      int     day;

                                      int     year;

                                      };

          struct           account{

                                                int     acct_no;

                                                char   acct_type;

                                                char   name[80];

                                                float   balance;

                                                struct date  lastpayment;

                     

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

C Language
To get help in C window (for keywords, functions) press Alt +F1.To delete a single line , use the shortcut key CTRL +Y.If you got error about the path when you execute a C pgm, check the Options menu =>Directories.
T

Thilagam S.

0 0
0

Static and dynamic libraries
A library is a package of code that is meant to be reused by many programs. A static library (also known as an archive) consists of routines that are compiled and linked directly into your program. When...

C Program-Error Handling[Program Exit Status]
//Header files #include<stdio.h>#include<conio.h>#include<stdlib.h> //Main Function void main(){ int dividend=20; int divisor=5; int quotient; //Function for clearing screen clrscr();...

Memory Layout of C Programs
A typical memory representation of C program consists of following sections. Text Segment: A text segment, also known as a code segment or simply as text, is one of the sections of a program in an object...

How do i get best Campus / Off Campus Placement?
Companies are looking for Skilled Freshers. So build your technical skills while doing MCA / BTech / BCA / BSc (IT or CS) into below areas- 1. Strong your programming & debugging skills ...

Looking for C Language Classes?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for C Language Classes?

The best tutors for C Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C Language with the Best Tutors

The best Tutors for C Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more