CS113 Introduction to C

12
CS113 Introduction to C Instructor: Ioannis A. Vetsikas E-mail: [email protected]. edu Lecture 6 : September 6

description

CS113 Introduction to C. Instructor: Ioannis A. Vetsikas E-mail: [email protected] Lecture 6 : September 6. Create your own types: typedef. #define N 3 typedef double scalar; typedef scalar vector[N]; typedef scalar matrix[N][N]; - PowerPoint PPT Presentation

Transcript of CS113 Introduction to C

Page 1: CS113 Introduction to C

CS113Introduction to C

Instructor: Ioannis A. VetsikasE-mail: [email protected]

Lecture 6 : September 6

Page 2: CS113 Introduction to C

2

Create your own types: typedef

#define N 3typedef double scalar;typedef scalar vector[N];typedef scalar matrix[N][N];

void add_vectors( vector x, vector y, vector z ){ int i; for( i = 0; i < N; i++ ) { x[i] = y[i] + z[i]; }}

Syntax: typedef <some type> <type name>Way to remember it: typedef <same as a declaration>

Page 3: CS113 Introduction to C

3

Structures

struct card_struct

{ int pips;

char suit;

};

typedef struct card_struct card;

• The structure mechanism allows us to aggregate elements of different types• Think of objects (but not quite!)

• Your struct definition generally goes outside all functionsstruct card_struct

{ int pips;

char suit;

} c1, c2;

typedef struct card_struct card;

void some_function()

{

struct card_struct a;

card b; /* a, b have same types */

b.pips = 3;

}

Page 4: CS113 Introduction to C

4

Structures (continued)

• Syntax:struct [<name/tag>] {

component-declarations

} [<variable name(s)>];

• Access elements/fields with dot operator:– b.pips or a.suit

• Structure Names/Tags– If a structure type does not have a name, then only a

finite number of structures can be created

– Structure tags form their own namespace!

• Structure fields also form their own namespace…

Page 5: CS113 Introduction to C

5

Structures (continued)

• Once a structure is declared you can use it as though it were a default data type (e.g. int)struct point {int x,y;};

struct rect {struct point pt1, pt2;};

• Can return from and also pass structures to functions (call-by-value)int ptinrect (struct point p, struct point r)

{ return p.x>=r.pt1.x && p.x<r.pt2.x

&& p.y>=r.pt1.y && p.y<r.pt2.y

}

Page 6: CS113 Introduction to C

6

Structures (continued)• Another example of a function:

struct point midpoint (struct point a, struct point b)

{ struct m = {(a.x+b.x)/2, (a.y+b.y)/2}; return m;}

• Usually one uses typedef to name the struct in some way and thus does not have to put struct <tag> all the time

• What does a=b do, when we have definedstruct point a=b ?

• Can also define pointers to structures in the same way as for simple data types:– struct point pt, *point_ptr;

• We can use the &,* operators in the same way as before:– *point_ptr or &pt

Page 7: CS113 Introduction to C

7

Structures – accessing via pointerIf p is a pointer to a structure and x is an element of the structure then to access

this element one puts: (*p).x or more commonly p->x

struct card_struct

{ int pips;

char suit;

};

typedef struct card_struct card;

void set_card( card *c )

{ c->pips = 3;

c->suit = ‘A’;

}

void main()

{ card a;

set_card( &a );

}

Page 8: CS113 Introduction to C

8

Pointers to Structures (continued)

• Call by value: expensive (and slow) to pass structures between functions– Use pointer instead (this simulates call by

reference)

• Operators -> and . are left-to-right associative and have maximum precedence along with () and []– e.g. ++pp->x increments field x, not pointer

pp

Page 9: CS113 Introduction to C

9

Structures – initializeYou may initialize a variable corresponding to a structure that was

defined by initializing all its elements as follows:struct name var = {init_element_1, …, init_element_n}#include <stdio.h>

struct address_struct

{ char *street;

char *city_and_state;

long zip_code;

};

typedef struct address_struct address;

void main()

{

address a = { "1449 Crosby Drive", "Fort Washington, PA", 19034 };

}

Page 10: CS113 Introduction to C

10

Unions• Similar to structures, but they can only hold one of the

elements at a time• So they use the same spot in memory to save any of the

possible elements.• Memory for union is max of memory needed for each

elementunion int_or_float

{

int i;

float f;

};

union int_or_float a;

/* need to keep track of, on own, which type is held */

/* a.i always an int, a.f always a float */

Page 11: CS113 Introduction to C

11

sizeof Operator• sizeof expression/object or sizeof <type name> returns the size of the type of the expression or the type in bytes– the expression is not evaluated– E.g. sizeof a, sizeof a[13] (a is array)

or sizeof int– Typing sizeof a/sizeof a[0]

returns?

• Cannot be applied to functions

Page 12: CS113 Introduction to C

12

Read from K&R• Covered 6.1-6.4 today

Other issues• Homework 2 due on Friday

• You MUST print the output exactly in the same way as in the examples which are given

• Do not forget to include your name and student ID# when you submit your programs by email + give me printout