Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Chapter 10 - C structures and unions, PPT, PF, Semester, Engineering

Chapter 10 - C structures and unions, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE) PDF Download

Introduction

 

The slides cover the following topics:

  1. C structures
  2. Strucuture Declarations
  3. Recursively defined structures
  4. Member access
  5. Memory layout
  6. Bit fields
  7. Structures as function arguments
  8. Unions

 

 

C structures and unions---------------------------------------------Next slide

 

C structures: aggregate, yet scalar

 

•aggregate in that they hold multiple data items at one time

   •named members hold data items of various types

   •like the notion of class/field in C or C++

  – but without the data hiding features

 

•scalar in that C treats each structure as a unit
   •as opposed to the “array” approach: a pointer to a collection of members in memory
   •entire structures (not just pointers to structures) may be passed as function arguments, assigned   to variables, etc.
   •Interestingly, they cannot be compared using ==

  (rationale: too inefficient)

 

C structures and unions---------------------------------------------Next slide

 

Structure declarations

 

•Combined variable and type declaration

struct tag {member-list} variable-list;

 

•Any one of the three portions can be omitted

struct {int a, b; char *p;} x, y;  /* omit tag */

 

•variables x, y declared with members as described:

  int members a, b and char pointer p.

•x and y have same type, but differ from all others –

  even if there is another declaration:

struct {int a, b; char *p;} z;

  /* z has different type from x, y */

 

C structures and unions---------------------------------------------Next slide

 

Structure declarations

 

struct S {int a, b; char *p;};  /* omit variables */

•No variables are declared, but there is now a type struct S that can be referred to later

 

struct S z;  /* omit members */

•Given an earlier declaration of struct S, this declares a variable of that type

 

typedef struct {int a, b; char *p;} S;

  /* omit both tag and variables */

•This creates a simple type name S

  (more convenient than struct S)

C structures and unions---------------------------------------------Next slide

 

Recursively defined structures

 

•Obviously, you can’t have a structure that contains an instance of itself as a member – such a data item would be infinitely large
 
•But within a structure you can refer to structures of the same type, via pointers

struct TREENODE {

  char *label;

  struct TREENODE *leftchild, *rightchild;

}

 

C structures and unions---------------------------------------------Next slide

 

Recursively defined structures

 

•When two structures refer to each other, one must be declared in incomplete (prototype) fashion

struct HUMAN;

struct PET {

  char name[NAME_LIMIT];

  char species[NAME_LIMIT];

  struct HUMAN *owner;

Chapter 10 - C structures and unions, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

C structures and unions---------------------------------------------Next slide

 

Member access

 

Chapter 10 - C structures and unions, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

C structures and unions---------------------------------------------Next slide

 

Memory layout

 

struct COST { int amount;

              char currency_type[2]; }

struct PART { char id[2];

              struct COST cost;

              int num_avail; }

Chapter 10 - C structures and unions, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

C structures and unions---------------------------------------------Next slide

 

Memory layout

 

A better alternative (from a space perspective):

struct COST { int amount;

              char currency_type; }

struct PART { struct COST cost;

              char id[2];

              int num_avail;

Chapter 10 - C structures and unions, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

C structures and unions---------------------------------------------Next slide

 

Bit fields

 

Chapter 10 - C structures and unions, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

C structures and unions---------------------------------------------Next slide

 

Bit fields

 

Chapter 10 - C structures and unions, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

C structures and unions---------------------------------------------Next slide

 

Structures as function arguments

 

•Structures are scalars, so they can be returned and passed as arguments – just like ints, chars

struct BIG changestruct(struct BIG s);

•Call by value: temporary copy of structure is created
•Caution: passing large structures is inefficient

  – involves a lot of copying

•avoid by passing a pointer to the structure instead:

void changestruct(struct BIG *s);

•What if the struct argument is read-only?
•Safe approach: use const

void changestruct(struct BIG const *s);

 

C structures and unions---------------------------------------------Next slide

 

Unions

 

•Like structures, but every member occupies the same region of memory!
 
•Structures: members are “and”ed together: “name and species and owner”
 
•Unions: members are “xor”ed together

 

union VALUE {

  float f;

  int i;

  char *s;

};

/* either a float xor an int xor a string */

 

C structures and unions---------------------------------------------Next slide

 

Unions

 

•Up to programmer to determine how to interpret a union (i.e. which member to access)
 
•Often used in conjunction with a “type” variable that indicates how to interpret the union value

Chapter 10 - C structures and unions, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

C structures and unions---------------------------------------------Next slide

 

Unions

 

•Storage
 
•size of union is the size of its largest member
 
•avoid unions with widely varying member sizes;

  for the larger data types, consider using pointers instead

 

•Initialization
•Union may only be initialized to a value appropriate for the type of its first member
The document Chapter 10 - C structures and unions, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE) is a part of Computer Science Engineering (CSE) category.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)

Top Courses for Computer Science Engineering (CSE)

FAQs on Chapter 10 - C structures and unions, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

1. What are C structures and unions?
Ans. C structures and unions are data types that allow us to group together different variables of different data types into a single unit. Structures are used to create a user-defined data type that can hold multiple variables of different types, while unions allow multiple variables to share the same memory location.
2. How do we declare and define a structure in C?
Ans. To declare and define a structure in C, we use the "struct" keyword followed by the structure name, and then list the variables and their data types within curly braces. For example: ``` struct Student { char name[50]; int age; float gpa; }; ``` This defines a structure named "Student" with variables "name" (of type char array), "age" (of type int), and "gpa" (of type float).
3. What is the difference between a structure and a union in C?
Ans. The main difference between a structure and a union in C is how the variables within them are stored in memory. In a structure, each variable has its own memory space, whereas in a union, all variables share the same memory location. This means that a union can only hold the value of one variable at a time, while a structure can hold the values of all its variables simultaneously.
4. How can we access the variables within a structure in C?
Ans. To access the variables within a structure in C, we use the dot (.) operator. We write the structure variable name followed by a dot and then the variable name. For example, if we have a structure variable "student1" of type "Student" (as defined in question 2), we can access the "name" variable using "student1.name", the "age" variable using "student1.age", and the "gpa" variable using "student1.gpa".
5. What are some applications of structures and unions in C programming?
Ans. Structures and unions are widely used in C programming for various applications. Some common applications include: - Creating complex data structures to hold related information, such as a database record or a student record. - Passing multiple arguments to functions by using structures as function parameters. - Storing different types of data in a single memory location using unions. - Implementing data serialization and deserialization techniques. - Interfacing with hardware devices by defining structures that represent the device registers.
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

past year papers

,

Engineering - Computer Science Engineering (CSE)

,

study material

,

PPT

,

Summary

,

Semester

,

Free

,

MCQs

,

Engineering - Computer Science Engineering (CSE)

,

Semester

,

Semester Notes

,

Chapter 10 - C structures and unions

,

ppt

,

Objective type Questions

,

pdf

,

Semester

,

Chapter 10 - C structures and unions

,

Extra Questions

,

Exam

,

shortcuts and tricks

,

PF

,

PF

,

Sample Paper

,

PPT

,

video lectures

,

Important questions

,

Viva Questions

,

PF

,

practice quizzes

,

PPT

,

Chapter 10 - C structures and unions

,

Previous Year Questions with Solutions

,

mock tests for examination

,

Engineering - Computer Science Engineering (CSE)

;