0% found this document useful (0 votes)
14 views9 pages

7 Structs

This document discusses structs and enumerations in C programming language. It defines structs as user-defined data types that allow grouping of different data types together. Structs can contain integer, floating point, character and enumerated data members. Individual members of a struct variable can be accessed using the dot operator. The document also discusses typedef which allows creating aliases for structs for easier declaration and use of struct variables. A sample program demonstrates creating nested structs and accessing members.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views9 pages

7 Structs

This document discusses structs and enumerations in C programming language. It defines structs as user-defined data types that allow grouping of different data types together. Structs can contain integer, floating point, character and enumerated data members. Individual members of a struct variable can be accessed using the dot operator. The document also discusses typedef which allows creating aliases for structs for easier declaration and use of struct variables. A sample program demonstrates creating nested structs and accessing members.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Programming Language

Structs and Enumeration


Data Structures (struct)
• Arrays require that all elements be of the same data type.
• Many times it is necessary to group information of different data
types.
• An example is a materials list for a product. The list typically
includes a name for each item, a part number, dimensions, weight,
and cost.
• C support data structures that can store combinations of character,
integer floating point and enumerated type data. They are called a
structs.
Structures (struct)
• A struct is a derived data type composed of members that are each
fundamental or derived data types.

• A single struct would store the data for one object. An array of
structs would store the data for several objects.

• A struct can be defined in several ways as illustrated in the


following examples:
Declaring Structures (struct)
Does Not Reserve Space Reserves Space

struct my_example struct my_example


{
{
int label;
int label; char letter;
char letter; char name[20];
char name[20]; } mystruct ;
};
/* The name "my_example" is called
a structure tag */
User Defined Data Types (typedef)
• The C language provides a facility called typedef for creating
synonyms for previously defined data type names. For example,
the declaration:

typedef int Length;

makes the name Length a synonym (or alias) for the data type int.
• The data “type” name Length can now be used in declarations in
exactly the same way that the data type int can be used:

Length a, b, len ;
Length numbers[10] ;
Typedef & Struct
• Often, typedef is used in combination with struct to declare a
synonym (or an alias) for a structure:

typedef struct /* Define a structure */


{
int label ;
char letter;
char name[20] ;
} Some_name ; /* The "alias" is Some_name */

Some_name mystruct ; /* Create a struct variable */


Accessing Struct Members
Individual members of a struct variable may be accessed using the
structure member operator (the dot, “.”):
mystruct.letter ;

Or , if a pointer to the struct has been declared and initialized


Some_name *myptr = &mystruct ;
by using the structure pointer operator (the “->“):
myptr -> letter ;
which could also be written as:
(*myptr).letter ;
Sample Program With Structs
/* This program illustrates creating structs and then declaring and using
struct variables. Note that struct personal is an included data type in
struct "identity".
*/
#include <stdio.h>
struct personal //Create a struct but don’t reserve space.
{ long id;
float gpa;
} ;
struct identity //Create a second struct that includes
the first one.
{ char name[30];
struct personal person;
} ;
Sample Program With Structs
(cont.)
int main ( )
{
struct identity js = {"Joe Smith"}, *ptr = &js ;

js.person.id = 123456789 ;
js.person.gpa = 3.4 ;
printf ("%s %ld %f\n", js.name, js.person.id,
js.person.gpa) ;
printf ("%s %ld %f\n", ptr->name, ptr->person.id,
ptr->person.gpa) ;
}

You might also like