Docsity

Structures and Unions in C Programming: A Comprehensive Guide, Lecture notes of C programming

Partial preview of the text

Download Structures and Unions in C Programming: A Comprehensive Guide and more Lecture notes C programming in PDF only on Docsity!

Chapter 5

Structure and Union

FE – C Programming

Prof. Namrata Jiten Patel

Assistant Professor Dept. of Computer Engineering, SIES Graduate School of Technology

Objectives :To provide exposure to problem-solving by developing an algorithm, flowchart and implement the logic using C programming language Outcomes: Learner will be able to …

  1. Demonstrate the use of structures and Union in C language.

Learning Objectives

  • (^) To learn concepts of Union and structure
  • (^) To learn Declaration and initialization of Union and structures
  • (^) To learn Nested structures
  • (^) To learn Array of structures
  • (^) To learn Passing structure to functions

Introduction

  • (^) A user-defined data type may also be called a derived data type.
  • (^) The array type is a derived data type that contains only one kind of fundamental data types defined in C.
  • (^) This means that the array elements, represented by a single name, contain homogeneous data.
  • (^) If the different elements in this cluster, known as array, are to be of different data types. Such nonhomogeneous data cannot be grouped to form an array.
  • (^) To tackle this problem suitably, C provides features to pack heterogeneous data in one group, bearing a user-defined data type name, and forming a conglomerate data type.
  • (^) So, C provides facilities for the user to create a new data type called the ‘structure’ that is capable of holding data of existing type.
  • (^) A structure is a collection of variables under a single name.
  • (^) These variables can be of different types, and each has a name which is used to select it from the structure.
  • (^) Therefore, a structure is a convenient way of grouping together several pieces of related information.
  • (^) Thus, a structure can be defined as a new named type, thus extending the number of available data types.
  • It can use other structures, arrays, or pointers as some of its members, though this can get complicated unless one is careful.
  • (^) A structure provides a means of grouping variables under a single name for easier handling and identification.
  • (^) Complex hierarchies can be created by nesting structures.
  • (^) Structures may be copied to and assigned.
  • (^) They are also useful in passing groups of logically related data into functions.

Declaring Structures and Structure Variables

  • (^) A structure is declared by using the keyword struct followed by an optional structure tag followed by the body of the structure.
  • (^) The variables or members of the structure are declared within the body.
  • There are three different ways to declare and/or define a structure. Variable structure Tagged structure Type-defined structure
  • (^) A variable structure may be defined as follows. struct { member_list }variable_identifier;
  • As an example the following statement is a definition of a variable structure:
  • (^) It does not offer any advantage over other declaration formats.
  • (^) A tagged structure has been described earlier.
  • (^) It has the following format:
  • (^) The preceding structure declaration may be expressed as a tagged structure as follows:

Example

  1. struct personal_data { char name[100]; char address[200]; int year_of_birth; int month_of_birth; int day_of_birth; }; struct personal_data monish, venkat, naresh;
  • The above statement is for defi ning a type of variable that holds a string of 100 characters called name, a string of 200 characters called address, and three integers called year_of_birth, month_of_birth, and day_of_birth.
  • Any variable declared to be of type struct personal_data will contain these components, which are called members.
  • (^) Different structures, even different types of structures, can have members with the same name, but the values of members of different structures are independent of one another.
  • (^) The same name for a member as for an ordinary variable in that program can also be used, but the computer will recognize them as different entities, with different values.
  • (^) This is similar to the naming convention for humans, where two different men may share the name ‘Jogi Sharma’, but are recognized as being different people.
  1. struct country { char name[30]; int population; char language[15]; }Country;
  • (^) Here, a structure variable Country has been declared to be of structure type country. This structure variable holds a string called name having 30 characters, an integer variable population and a string called language with 15 characters. 3) struct country { char name[30]; int population; char language[15]; }India, Japan, Indonesia; - (^) This structure named country has three structure variables India, Japan, Indonesia. - (^) All three structure variables hold the same kind of member elements, though with different values.

Points to note

  1. A structure can be defined as a user-defined data type that is capable of holding heterogeneous data of basic data type.

  2. The structure is simply a type template with no associate storage.

  3. The proper place for structure declarations is in the global area of the program before main().

  4. It is not possible to compare structures for equality using ‘==’, nor is it possible to perform arithmetic on structures.

For example, in

  • (^) The first member can be accessed by the construct s1.a
  • (^) For assigning a value of 12, say, to the member b of the structure identified by the variable s2, the following statement is written s2.b = 12;
  • (^) To print this value assigned to the member on thescreen, the following code is written. printf(“%d”, s2.b);
  • (^) Now, consider the structure given as follows. struct personal_data { char name[100]; char address[200]; int year_of_birth; int month_of_birth; int day_of_birth; };
  • and the declaration statement for the structure variables monish, venkat, and naresh is given by struct personal_data monish, venkat, naresh;