C Programming - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

C Programming

Description:

What is a Structure. How are Structures ... Stdout stream (the CRT Cathode Ray Tube a fancy acronym for MONITOR) ... hint, it has to do with the letter 'F' ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 22
Provided by: markcor
Category:

less

Transcript and Presenter's Notes

Title: C Programming


1
C Programming
  • MIS 3013 003
  • Mark Cornelius
  • Lecture 10 Structures, Unions More

2
Topics
  • What is a Structure
  • How are Structures implemented
  • How do Structures Work
  • The interesting details
  • What is a union
  • More about input and output
  • More about Strings

3
Topics cont.
  • Miscellaneous functions with strings

4
Structures
  • A collection of variables
  • Components, or members may be of different
    types (even arrays)
  • The members are totally at the programmers
    discretion
  • Most similar to a record in a database table.

5
How are Structures Implemented?
  • Structures must be defined!!!
  • Here is an example
  • Struct listing
  • Int id_no
  • Char lname20, fname20 e_mail20
  • Now we have a structure called listing
  • But we dont have any listings, because we did
    not declare any

6
Implemetation
  • We must now declare some variables that are of
    type listing.
  • Struct listing record1, record2
  • Now we have two variables that are of the
    structure we defined earlier
  • We can address the members of the structure
    individually..

7
Implementation
  • Each listing we created, record1 record2,
    have members, i.e.
  • Record1.id_no
  • Record1.lnamex
  • Record1.fnamex
  • Record1.e_mailx
  • Which are of the types specified in the
    definition of the structure.

8
What does this mean?
  • Now we can group related data under a SINGLE
    entity, regardless of the type!
  • Note the similarity between a structure and a
    database record.
  • Structures are the foundation for using linked
    lists.

9
Structures and Arrays
  • You can create an array of structures
  • You can create a multidimensional array of
    structures (but why would you?)
  • You can create pointers to structures
  • You can create pointers to structures, that are
    MEMBERS of the structure being pointed to (this
    is how linked lists work)

10
How do structures work
  • Members of structures are accessed individually
    (similar to arrays
  • You can perform any operation on a structure
    member that is legal for that type of data.
  • You can pass structures, or individual members to
    functions

11
Unions
  • Unions are almost Identical to structures
  • They have one key difference
  • Only one member of a union can be accessed at a
    time
  • This is because the members are stored together
    in memory
  • With that said..

12
DONT USE UNIONS
  • that is, if you dont have to.
  • I have spoken!

13
Your mission
  • To create an array of 5 structures that contains
    the following members
  • Id_number (integer)
  • Gpa (floating point)
  • Read in the data from the keyboard.
  • Display the result.
  • You have 20 Minutes

14
More on I/O
  • Thats Input Output, my friend!

15
More on input output
  • When reading data from the keyboard, and
    displaying to the screen, we are working with 2
    files
  • Stdin stdout
  • These are called streams
  • There are more streams, I.e. stderr, stdprn (the
    printer on lpt1)

16
Standard Input
  • Stdin stream
  • Reading data from the keyboard
  • Gets(), scanf(), getc(), getchar() all default to
    the standard input stream
  • Fgetc() and fgets() are different why?

17
Standard Output
  • Stdout stream (the CRT Cathode Ray Tube a
    fancy acronym for MONITOR)
  • Puts(), printf(), putc(), putchar() all default
    to the standard output stream
  • Fputc(), fputs() are different why? (hint, it
    has to do with the letter F)
  • You must learn the use of these commands, and
    they are explained in gory detail in chapter 14

18
More about Strings
  • Id stick around for this (especially if I
    planned on taking C)

19
More about Strings
  • We can do a lot of things with strings.we can
  • Parse
  • Search
  • Concatenate
  • Bring to uppercase
  • Bring to lowercase
  • Test for various conditions
  • Determine the actual length of the string

20
For example
  • If lname20 is an array of characters, I.e. a
    string, and is all uppercase
  • Strlwr(lname) makes it all lowercase
  • We could even use isupper and test it.
  • There are too many of these functions to
    memorize, which is why every sensible programmer
    always keeps a syntax guide handy!!!!

21
What is the point?
  • Point is, there are times when these functions
    come in handy, especially in PRODUCTION Systems
Write a Comment
User Comments (0)
About PowerShow.com