0% found this document useful (0 votes)
24 views17 pages

Advanced Data Types

This document discusses advanced data types in C, specifically structures. It defines what structures are, how to declare structure variables, access structure elements, initialize and assign structures. It also covers passing structures as function arguments, arrays of structures, initializing structure arrays, pointers to structures, and using the typedef keyword. The overall purpose is to explain how to use structures in C programming.

Uploaded by

thegamernothing
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)
24 views17 pages

Advanced Data Types

This document discusses advanced data types in C, specifically structures. It defines what structures are, how to declare structure variables, access structure elements, initialize and assign structures. It also covers passing structures as function arguments, arrays of structures, initializing structure arrays, pointers to structures, and using the typedef keyword. The overall purpose is to explain how to use structures in C programming.

Uploaded by

thegamernothing
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

FPT SOFTWARE WORKFORCE ASSURANCE

Advanced Data types


HoangND1

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1


Objectives - 1

 Explain structures and their use


 Define structures
 Declare structure variables
 Explain how structure elements are accessed
 Explain how structures are initialized
 Explain how assignment statements are used with
structures

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1


Objectives - 2

 Explain how structures can be passed as


arguments to functions
 Use arrays of structures
 Explain the initialization of structure arrays
 Explain pointers to structures
Explain how structure pointers can be passed as
arguments to functions

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1


Structures
 A structure consists of a number of data items, which need
not be of the same data type, grouped together
 The structure could hold as many of these items as desired

1 I I L L U S I O N B A C H 1
Variable L
L
U Name of the book Author
Edition
S
I
O
N Array

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1


Defining a Structure

 A structure definition forms a template for creating


structure variables
 The variables in the structure are called structure
elements or structure members
 Example:
struct cat
{ char bk_name [25];
char author [20];
int edn;
float price;
};

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1


Declaring Structure Variables
 Once the structure has been defined, one or more
variables of that type can be declared
 Example: struct cat books1;
 The statement sets aside enough memory to hold all
items in the structure

struct cat { char bk_name[25]; struct cat books1, books2;


char author[20];
int edn;
or
struct cat books1;
float price;
} books1, books2; struct cat books2;

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1


Accessing Structure Elements

 Structure elements are referenced through the use of the


dot operator (.), also known as the membership
operator
 Syntax:
structure_name.element_name
 Example:
scanf(“%s”, books1.bk_name);

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1


Initializing Structures
 Like variables and arrays, structure variables can be
initialized at the point of declaration
struct employee
{ int no;
char name [20];
};
 Variables emp1 and emp2 of the type employee can
be declared and initialized as:
struct employee emp1 = {346, “Abraham”};
struct employee emp2 = {347, “John”};

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1


Assignment Statements Used
with Structures-1

 It is possible to assign the values of one structure variable


to another variable of the same type using a simple
assignment statement

 For example, if books 1 and books2 are structure


variables of the same type, the following statement is
valid
books2 = books1;

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1


Assignment Statements Used
with Structures - 2

 In cases where direct assignment is not possible, the in-


built function memcpy() can be used

 Syntax:
memcpy (char * destn, char &source, int nbytes);

 Example:
memcpy (&books2, &books1, sizeof(struct cat));

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1


Structures within Structures

 It is possible to have one structure within another structure.


A structure cannot be nested within itself
struct issue
{
char borrower [20];
char dt_of_issue[8];
struct cat books;
}issl;
 To access the elements of the structure the format will be similar to
the one used with normal structures,
[Link]
 To access elements of the structure cat, which is a part of another
structure issue,
[Link]

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1


Passing Structures as
Arguments

 A structure variable can be passed as an


argument to a function

 This facility is used to pass groups of logically


related data items together instead of passing
them one by one

 The type of the argument should match the type


of the parameter

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1


Array of Structures

 A common use of structures is in arrays of structures


 A structure is first defined, and then an array variable of
that type is declared
 Example:

struct cat books[50];

 To the access the variable author of the fourth element of


the array books:

books[4].author

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1


Initialization of Structure
Arrays

 Structure arrays are initialized by enclosing the list of


values of its elements within a pair of braces
 Example:
struct unit
{ char ch;
int i;
};

struct unit series [3] =


{ {‘a’, 100}
{‘b’, 200}
{‘c’, 300}
};

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1


Pointers to Structures

 Structure pointers are declared by placing an asterisk(*)


in front of the structure variable’s name
 The -> operator is used to access the elements of a
structure using a pointer
 Example:
struct cat *ptr_bk;
ptr_bk = &books;
printf(“%s”, ptr_bk->author);
 Structure pointers passed as arguments to functions
enable the function to modify the structure elements
directly

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1


The typedef keyword

 A new data type name can be defined by using the


keyword typedef
 It does not create a new data type, but defines a new
name for an existing type
 Syntax:
typedef type name;
 Example:
typedef float deci;
 typedef cannot be used with storage classes

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1


Q&A

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/1

You might also like