Aggregate Data Types - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Aggregate Data Types

Description:

... best ever!'); the character array result ends up being: 'the result is the best ever! ... Designed to hold data of different scalar types. Structure Declaration ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 19
Provided by: gae5
Category:

less

Transcript and Presenter's Notes

Title: Aggregate Data Types


1
Aggregate Data Types
Structuring the Data
2
Aggregate Data Types
  • Data Types that are designed to hold multiple
    values.
  • Examples Arrays
  • Strings
  • Structures

3
One Dimensional Arrays
  • Used to store collections of data that are the
    same scalar type
  • int array_110
  • declaration of an array variable that will hold
    10 integers in positions from 0 to 9

4
Accessing Array Positions
  • Each array position is referenced through an
    index or subscript.
  • array_10 15
  • references position 0 of array_1

5
Initializing an Array
  • You can assign initial values to the positions in
    an array when the array is declared.
  • char array_220 Hello there!
  • initializes the first 11 positions of array_2 to
    the string Hello there!.

6
Strings
  • A string is simply a character array with a \0
    at the end (NULL or zero terminated).
  • String constants must be enclosed in double
    quotes (Hello there).
  • In C, a string must be created by assigning each
    individual position of the array with a character
    or using the C library functions.

7
strcpy()
  • strcpy copies from one string (or a string
    constant) to another string and adds \0.
  • char deststr20, sourcestr20
  • strcpy(deststr, sourcestr)
  • strcpy(deststr, Hello there)

8
strncpy()
  • strncpy copies from one string (or a string
    constant) to another string a specified (n)
    number of bytes and adds a \0.
  • char deststr20
  • sourcestr20 John Doe
  • strncpy(deststr, sourcestr, 4)

9
strlen()
  • strlen() returns the length of a string
  • length strlen(my_string)
  • strlen() can be used inside other function calls
  • printf(The length is d, strlen(my_string) )

10
strcat()
  • strcat() concatonates two strings, one onto the
    end of the other.
  • char result20 the result is
  • strcat( result, the best ever!)
  • the character array result ends up being
    the result is the best ever!

11
STRUCTURES
  • Designed to hold data of different scalar types

12
Structure Declaration
  • Example struct auto_part_tagS
  • char id8
  • float price
  • int inv_num

13
YOU Create a New Data Type
  • Using the keyword struct followed by a tag and
    your definition of a new structure - is a
    structure declaration - it only creates a new
    data type. You still have not declared any
    variables of this type.
  • Declare new structure data types globally, above
    main!

14
Using Your New Data Type
  • struct auto_part_tagS part1, part2, part3
  • declares variables called part1, part2 and part3
    - they are each variables of the type of
    structure you created.
  • Now, the variable part1 has room for an id, a
    price and an inventory count. So does part2 and
    part3.

15
Accessing Members in a Structure
  • Dot notation
  • part1.inv_num gives access to the cur_inv
    member of the variable part1
  • part1.inv_num 12
  • strcpy(part1.id, JB-104Q)
  • printf(This is inside a structure s, part1.id)

16
Use a Structure as a Database
  • Declare a structure data type above main
  • Then declare several variables of that type
  • This way, you can record data about many auto
    parts in stock, or many employees on your
    payroll, or many foot ball games held this
    season, etc...

17
Use a Structure to Pass Parameters
  • Declare a structure data type
  • Declare a variable of that data type.
  • Use the single variable to pass the whole
    structure as a parameter to another function.
  • My_function(part1)

18
LAB
Write a Comment
User Comments (0)
About PowerShow.com