0% found this document useful (0 votes)
15 views2 pages

Lesson 14 - Structure - Compatibility Mode

Structures allow grouping of different data types together. A struct can store combinations of character, integer, floating point, and enumerated type data. Structs are declared by specifying the member names and their data types. Individual members of a struct variable can then be accessed using the dot operator. An example program demonstrates creating structs, declaring struct variables, and accessing members.

Uploaded by

minhto123321
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)
15 views2 pages

Lesson 14 - Structure - Compatibility Mode

Structures allow grouping of different data types together. A struct can store combinations of character, integer, floating point, and enumerated type data. Structs are declared by specifying the member names and their data types. Individual members of a struct variable can then be accessed using the dot operator. An example program demonstrates creating structs, declaring struct variables, and accessing members.

Uploaded by

minhto123321
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/ 2

10/13/2021

DATA STRUCTURES (STRUCT)

STRUCTURES Arrays require that all elements be of the same

Nguyen Thi Thu Huong - SoiCT - HUST


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 and C++ support data structures that can store
combinations of character, integer floating point
Nguyen Thi Thu Huong
and enumerated type data. They are called a
Department of Computer Science structs.
School of Information and Communication Technology
Hanoi University of Technology 2

1 2

STRUCTURES (STRUCT) DECLARING STRUCTURES (STRUCT)


A struct is a derived data type composed of Does Not Reserve Space Reserves Space
members that are each fundamental or derived
data types.
Nguyen Thi Thu Huong - SoiCT - HUST

Nguyen Thi Thu Huong - SoiCT - HUST


struct my_example struct my_example
{ {
A single struct would store the data for one
int label; int label;
object. An array of structs would store the data
for several objects. char letter;
char letter;
char name[20];
char name[20];
A struct can be defined in several ways as } mystruct ;
};
illustrated in the following examples:
/* The name "my_example" is
called a structure tag */
3 4

3 4

USER DEFINED DATA TYPES (TYPEDEF) TYPEDEF & STRUCT


typedef : for creating synonyms for previously Often, typedef is used in combination with struct to declare a
defined data type names. synonym (or an alias) for a structure:
Example:
Nguyen Thi Thu Huong - SoiCT - HUST

Nguyen Thi Thu Huong - SoiCT - HUST

typedef struct /* Define a structure */


typedef int Length; {
Length is a synonym (alias) for the data type int. int label ;
char letter;
The data “type” name Length can now be used in char name[20] ;
declarations in exactly the same way that the } Some_name ; /* The "alias" is Some_name */
data type int can be used:
Length a, b, len ; Some_name mystruct ; /* Create a struct variable */
Length numbers[10] ; 5 6

5 6

1
10/13/2021

ACCESSING STRUCT MEMBERS


SAMPLE PROGRAM WITH STRUCTS
Individual members of a struct variable may be /* This program illustrates creating structs and
then declaring and using struct variables. Note
accessed using the structure member operator (the that struct personal is an included data type in
dot, “.”): struct "identity".

Nguyen Thi Thu Huong - SoiCT - HUST

Nguyen Thi Thu Huong - SoiCT - HUST


*/
mystruct.letter ; #include <stdio.h>
struct personal //Create a struct but don’t reserve
Or , if a pointer to the struct has been declared and space.
initialized { long id;
Some_name *myptr = &mystruct ; float gpa;
};
by using the structure pointer operator (the “->“):
struct identity //Create a second struct that
myptr -> letter ; includes the //first one.
which could also be written as: { char name[30];
(*myptr).letter ;
struct personal person;
7 }; 8

7 8

SAMPLE PROGRAM WITH STRUCTS (CONT.)


int main ( )
{
struct identity js = {"Joe Smith"}, *ptr = &js ;
Nguyen Thi Thu Huong - SoiCT - HUST

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