Structure, Combining Structure and Typedef - Low Level Programming | CMSC 212, Study notes of Computer Science

Material Type: Notes; Professor: Hollingsworth; Class: INTRO TO LOW-LEVEL PROG; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-uxf-1
koofers-user-uxf-1 🇺🇸

10 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 212 – S05 (lect 5)
Announcements
Program #1A
Due Today at 8pm
Program #1B
Due in two weeks
Will be available by 8pm tonight
Reading
Chapter 10 (for today, skip 10.2)
Chapter 5 (by next Tuesday)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Structure, Combining Structure and Typedef - Low Level Programming | CMSC 212 and more Study notes Computer Science in PDF only on Docsity!

Announcements

l^

Program #1A– Due Today at 8pm

l^

Program #1B– Due in two weeks– Will be available by 8pm tonight

l^

Reading– Chapter 10 (for today, skip 10.2)– Chapter 5 (by next Tuesday)

Structures

l^

Syntax: struct [tag] { member-list } [variable-list];– [ ] indicate optional portions depending on the version used– { } and ; are required

l^

Notes:– used to declare related items– similar to classes (without methods) in Java

l^

Example:struct {

int a;char b;float c; } x; •^

Declares a single variable x with 3 fields

Combining struct and typedef

l^

Often useful to use typedef and struct together:typedef struct {

int a;char b;float c; } Simple; l^

Declarations then look like:Simple x;Simple y[20], z; l^

Header Files–

If structure used by more than one file, put it in a header file.– If you want programmers who will be using your implementation to seethis struct, put it in the header file.– Use

#include

to include definition in each file where used.

Accessing Fields of Structures

l^

Consider this definition:struct {

int a;char b;float c; } x, y[10]; l^

Simple structure access:– x.a = 3;– printf("field b = %c\n", x.b);

l^

Accessing arrays of structures:– y[7].c = 3.14;– y[i].b = 'a';

Bit Fields

l^

Part of the Power of C is control over data layout– If you need a field with exactly 13 bits, you can define it– size must be less than or equal to the default size for that type– Useful for:

  • defining fields that interact with hardware– managing pre-defined file formats– saving every last bit of space
    • Syntax:

type

variablename:size;

l^

Examples:int foo:13;unsigned int foo:4;typedef struct {

/* from project #1 */

unsigned int opCode:4;unsigned int r1:4;unsigned int r2:4;unsigned int r3:4;unsigned int address:16;} instruction;

Passing Structures as Arguments

typedef struct { char productName[200];int quantity;float unitPrice; } Transaction;void printReceipt( Transaction trans){

printf("product = %s\n", trans.productName);printf("

quanity = %d\n", trans.quanity);

printf("

price = %f\n", trans.unitPrice);

} int main(void){Transaction foo;printReceipt(foo); l^

Uses call by value– Entire structure is copied to printReceipt

typedef struct { char productName[200];int quantity;float unitPrice; } Transaction;void initReceipt(Transaction *trans){

trans->productName[0] = ‘\0’;trans->quanity = 0;trans->unitPrice = 12.50; } int main(void){Transaction foo;initReceipt(&foo);

Passing a reference to a Structure

Indicates pointer type variable

Indicates pass the reference (the address)

Unions

l^

Syntax is similar to structs l^

Rather than storing each field, only stores

one

of the fields

-^

Handy when need to stores different things based on criteria

l^

Syntax:–

union [tag] { member-list } [variable-list];– again, the things in [ ] are optional– the { } and ; are required l^

Examples:union {

float f;int i; } objname;objname.f = 3.14159;

Memory Allocation

l^

Structures are placed in consecutive memory locations– memory is rounded up to alignment

  • characters take 1 byte• integers normally take 4 bytes and start at aligned addresses

l^

Can query the size of a structure or variable– sizeof(structure name) or sizeof(variable name)

0x000x040x080x0c0x100x

struct { int a;char b;int c; } x; x = { 3, 'a', 10 };

3 0 0 0

0x (^0) (^0) (^0)

? ?^

?

0xA

Project #1B

l^

int getFile(char *fileName, char file[MEM_SIZE][80]);– reads the passed fileName into the array file– returns the number of lines read– returns a –1 on error

  • can’t find the file• too many lines