Lecture 11 C Programming - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Lecture 11 C Programming

Description:

C Programming. L11.7. Using a struct. By defining a structure ... C Programming. L11.13. Sample Function (won't work!) void update_average(struct Student stu) ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 16
Provided by: arne69
Category:

less

Transcript and Presenter's Notes

Title: Lecture 11 C Programming


1
Lecture 11C Programming
  • Arne Kutzner

2
C Structures
3
Structure
  • A Structure is a container, it can hold a bunch
    of things.
  • These things can be of any type.
  • Structures are used to organize related data
    (variables) into a nice neat package.

4
Example - Student Record
  • Student Record
  • Name a string
  • HW Grades an array of 3 doubles
  • Test Grades an array of 2 doubles
  • Final Average a double

5
Structure Members
  • Each thing in a structure is called member.
  • Each member has a name, a type and a value.
  • Names follow the rules for variable names.
  • Types can be any defined type.

6
Example Structure Definition
  • struct Student
  • char name // student name
  • double hw3 // homework grades
  • double test2 // test grades
  • double ave // final average

7
Using a struct
  • By defining a structure you create a new data
    type.
  • Once a struct is defined, you can create
    variables of the new type.
  • struct Student stu

8
Accessing Members
  • You can treat the members of a struct just like
    variables.
  • You need to use the member access operator '.'
    (pronounced "dot")
  • printf("s", stu.name)
  • stu.hw2 82.3
  • stu.ave total/100

9
Structure Assignment
  • You can use structures just like variables
  • struct Student s1,s2
  • s1.name "Joe Student"
  • s2 s1

Copies the entire structure
10
Be Careful
  • If a member is a pointer, copying means copying
    the pointer (not what is pointed to).

"Joe Student"
name
hw
test
ave
11
Pointers to Structures
  • Pointers to structures are used often.
  • There is another member access operator used with
    pointers -gt
  • struct Student sptr
  • printf("Name is s", sptr-gtname)
  • printf("Ave is d", sptr-gtave)

it looks like a "pointer"!
12
Structures and Functions
  • Structures are copied if they are passed as
    function argument

void fun(struct Student stu) struct
Student s fun(s)
We copy the structure s !
13
Sample Function (won't work!)
  • void update_average(struct Student stu)
  • double tot0
  • for (int i0ilt3i)
  • tot stu.hwi
  • for (int i0ilt3i)
  • tot stu.testi
  • stu.ave tot/5

problematical !Why?
14
This one works
  • void update_average(struct Student stu)
  • double tot0
  • for (int i0ilt3i)
  • tot stu-gthwi
  • for (int i0ilt3i)
  • tot stu-gttesti
  • stu-gtave tot/5

15
Structures and malloc
  • The malloc function can be used for dynamic
    memory allocation with structures. Example

struct Student student student
malloc(sizeof(struct Student)) student-gtave
0 student-gtname "Kim" free(student)
Write a Comment
User Comments (0)
About PowerShow.com