0% found this document useful (0 votes)
9 views25 pages

Week 13

Uploaded by

bewahig513
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views25 pages

Week 13

Uploaded by

bewahig513
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

WEEK 13

C STRUCTURES,
UNIONS
C How to Program, 8/e, GE

© 2016 Pearson Education, Ltd. All rights reserved.


Create and structs, unions.
use

Learn about operations that can be performed on struct instances.

Initialize struct members.

OBJEC T I V E S
Access struct members.

struct instances to functions by value and by


Pass reference.

Learn about operations that can be performed on unions.


INTRODUCTION

◦ Structures are derived data types—they’re constructed using objects of other types.
Consider the following structure definition
struct card { Structure tag
char *face;
char *suit; };
• A database is a collection of information subdivided into records. A record is a collection
of information of one data object (e.g., ID, name, and age of a student).
• C allows us to define a new data type (called structure type) for each category of a
structured data object.
OPERATIONS THAT CAN BE PERFORMED ON STRUCTURES

The only valid operations that may be performed on structures are:


◦ Assigning struct variables to struct variables of the same type for a pointer member, this
copies only the address stored in the pointer.
◦ Taking the address (&) of a struct variable.
◦ Accessing the members of a struct variable.
◦ Using the sizeof operator to determine the size of a struct variable
We can reference a component

Manipulati of a structure by the direct


component selection
operator, which is a period.
ng
Structure E.g.,
strcpy(student1.name, “Chao”);
Types student1.age = 18;
printf(“%s is in age %d\n”,
student1.name, student1.age);
INITIALIZING STRUCTURES

Structures can be initialized using initializer lists as with arrays. To initialize a structure,
follow the variable name in the definition with an equals sign and a brace-enclosed,
comma- Separated list of initializers. For example, the declaration

struct card aCard = { "Three", "Hearts" };


Structures may be passed to
functions by

USING passing individual structure


members.
STRUCTURES
WITH
FUNCTIONS passing an entire structure.

passing a pointer to a
structure.
TYPEDEF
◦ The keyword typedef provides a mechanism for creating synonyms (or aliases) for
previously defined data types. Names for structure types are often defined with typedef
to create shorter type names. For example, the statement
typedef struct card Card;
◦ defines the new type name Card as a synonym for type struct card. C programmers often
use typedef to define a structure type, so a structure tag is not required. For example, the
following definition creates the structure type Card without the need for a separate
typedef statement
typedef struct { char *face; char *suit; } Card;
struct card {

char *face;

char *suit; };

int main(void)

struct card B ;

};

=======================================================================
=======================
typedef struct {
char *face;
char *suit;
} Card;
int main(void)
{
Card B ;
}
OPERATOR
PRECEDENCE
IN STRUCT
TYPE
COMPONENT
SELECTION
OPERATOR
A union definition has the same
format as a structure definition. The
union definition
union number { int x; double y; };

UNION
DECLARATIONS indicates that number is a union
type with members int x and double
y. The union definition is normally
placed in a header and included in
all source files that use the union
type.
OPERATIONS THAT CAN BE PERFORMED ON UNIONS

The operations that can be performed on a union are:


• assigning a union to another union of the same type.
• taking the address (&) of a union variable.
• accessing union members using the structure member operator and the structure
pointer operator.
Unions may not be compared using operators == and != for the same reasons that
structures cannot be compared.
void isequal(MY_STRUCT a, MY_STRUCT b)

{ if (a == b)

{ puts("equal");

} else

{ puts("not equal");

}}

The a==b comparison will AFAIK throw a compile error on any sensible C compiler,
because the C standard doesn't allow for built-in structure comparison. Workarounds using
memcmp are of course a bad idea due to alignment, packing, bitfields etc., so we end up
writing element by element comparison functions.
ARRAYS OF
STRUCTUR
ES
All the best

You might also like