You are on page 1of 4

CS 354 - Machine Organization

Monday, September 19, 2016

Homework hw2 (1.5%) assigned today, due 10 pm Friday, September 23th


Project p1 (3%) 10 pm TONIGHT Monday, September 19th
Project p2 (6%) assigned tomorrow

Last Time
Pointer Caveats
Parameter Passing and Pointers
Sharing Info between Functions
Command Line Arguments
Today
Return Values and Pointers (from last time)
Parameter and Return Value Caveats (from last time)
Structures
Nested structs and Arrays of structs
Pointers to structs
Next Time
Read: K&R Ch. 7
Console & File I/O
C Strings

Copyright 2016 Jim Skrentny

CS 354 (F16): L6 - 1

Structures
What?

Why?

How?
struct <struct-name> {
<data-declarations>;
} <optional-list-of-variables>;

 Declare a structure representing a date having a integer.month, day of month, and year.

 Create a variable containing todays date..

 Complete the function below that displays the a date structure..


void printDate (struct Date date) {

Typedef
what:
why:

 Update the code above to use typedef.

Copyright 2016 Jim Skrentny

CS 354 (F16): L6 - 2

Nested structs and Array of structs


Consider the code:
#include <stdio.h>
typedef struct Date ... //from previous page
typedef struct Pokemon {
char name[11];
char type[11];
float weight;
Date caught;
} Pokemon;
void printDate (Date date) ... //from previous page
void printPm(Pokemon
printf("\nPokemon
printf("\nPokemon
printf("\nPokemon
printf("\nPokemon
printf("\n");
}

pm) {
Name
Type
Weight
Caught on

:
:
:
:

%s",pm.name);
%s",pm.type);
%f",pm.weight);
", printDate(pm.caught));

int main(void) {
Pokemon pm1 = {"Abra","Psychic",30,{19,9,2016}};
printPm(pm1);

 Creafe an arry, named pokedex, initialized with three pokemon.

 Complete the function below so it displays a pokedex..


printDex(pokedex, 3);
void printDex(Pokemon dex[], int size) {

Copyright 2016 Jim Skrentny

CS 354 (F16): L6 - 3

Pointers to structs
Why?

How?

 Declare a pointer.

 Dynamically allocate space for a Pokemon.

 Assign values to the members of the structure.

 Dynamically deallocate space.

 Update the code on the previous page to efficiently pass and print a pokedex,.

Copyright 2016 Jim Skrentny

CS 354 (F16): L6 - 4

You might also like