1. Introduction Lecture 15 Lecture 15 2. B inary R epresentation Data Structures 3. H ardw are and S oftw are 4. H igh Level Languages • One use of functions is that of procedural abstraction 5. S tandard input and output Page 81 of notes 6. Operators, expression and statem ents – Breaking a complicated program into smaller manageable 7. M aking Decisions pieces 8. Looping – This is knows as structured programming 9. A rrays 10. B asics of pointers • Programs consist of both algorithms and data and 11. S trings structures 12. B asics of functions • C provides a way of structuring complicated data into 13. M ore about functions neat units too - the data structure 14. Files 15. D ata S tructures 16. C ase study: lottery num ber generator Data Structures Data Structures struct cplx{ • A simple example is a complex number double real; double imag; • Complex numbers consist of }; – a real part • This creates a new structured data type – an imaginary part called struct cplx which we can use in • We could represent this using an array of size 2 a similar way to other data types ( int , – double array[2] char etc) • Or we can declare a data structure • e.g. we can declare and initialise variables struct cplx{ keyword double real; of this type: structure members double imag; “tag” struct cplx a, b={1.0, -5.3}, c; }; Data Structures Structure Members • Actually the “tag” is optional (but useful) so if • The structure members can be a mixture of we want to declare a single structured different data types, including simple types ( int , long , double , etc) arrays, pointers, variable we can omit it. strings and other structs • We can also combine this with the initialisation • We can then refer to members using a dot notation e.g. struct { double real; p.thing=17; q.other=p.other/3.2; no “tag” double imag; (an int ) (a float ) (a float ) } p={57,9.2}, q; • However, we now cant use this structure to define further variables of the same type. 1
Operations on Structures Structure Members • The only legal operations on a structure are – accessing its members (see last slide) /* Example: using structures to represent complex numbers */ /* Example: using structures to represent complex numbers */ – copying or assigning to it #include <stdio.h> #include <stdio.h> – taking its address complex1.c complex1.c void main(void) • In the previous example we could have written q=p ; then q void main(void) { { pg 81 struct cplx { pg 81 is an individual copy of p struct cplx { double real; /* real part */ double real; /* real part */ double imag; /* imaginary part */ double imag; /* imaginary part */ – we don’t have to copy the members of the structure individually the }; }; compiler will do this for us struct cplx x = {2.5, 5.0}, y = {3.2, -1.7}, z; struct cplx x = {2.5, 5.0}, y = {3.2, -1.7}, z; • Remember that z.real = x.real + y.real; /* add real parts */ z.real = x.real + y.real; /* add real parts */ z.imag = x.imag + y.imag; /* add imaginary parts */ z.imag = x.imag + y.imag; /* add imaginary parts */ – calling a function by value includes copying its arguments printf("z = %4.2f + %4.2f j\n", z.real, z.imag); printf("z = %4.2f + %4.2f j\n", z.real, z.imag); – returning a value also includes a copy process return; return; • Structures are therefore a very convenient way of passing } } z = 5.70 + 3.30 j z = 5.70 + 3.30 j information into and out of functions /* Example: structures as function arguments and return values */ /* Example: structures as function arguments and return values */ Scope of Structures #include <stdio.h> #include <stdio.h> complex2.c complex2.c struct cplx { struct cplx { double real; /* real part */ double real; /* real part */ pg 82 pg 82 double imag; /* imaginary part */ double imag; /* imaginary part */ }; }; • The scope rules for struct are similar to struct cplx add(struct cplx a, struct cplx b); /* function prototype */ struct cplx add(struct cplx a, struct cplx b); /* function prototype */ those for variables void main(void) void main(void) { { • A struct declared within a block (including struct cplx x = {2.5, 5.0}, y = {3.2, -1.7}, z; struct cplx x = {2.5, 5.0}, y = {3.2, -1.7}, z; function body) is visible only within that block z = add(x, y); z = add(x, y); printf("z = %4.2f + %4.2f j\n", z.real, z.imag); printf("z = %4.2f + %4.2f j\n", z.real, z.imag); return; return; • A struct declared at the start of a program, } } outside any block is visible throughout the struct cplx add(struct cplx a, struct cplx b) struct cplx add(struct cplx a, struct cplx b) { program i.e. it is global { struct cplx c = a; /* can initialise an auto struct variable */ struct cplx c = a; /* can initialise an auto struct variable */ – This is useful where a struct is to be passed to c.real += b.real; c.real += b.real; c.imag += b.imag; c.imag += b.imag; return c; /* can return a struct value */ functions return c; /* can return a struct value */ } } z = 5.70 + 3.30 j z = 5.70 + 3.30 j Pointers to Structures Pointers to Structures • A pointer to a struct can be created and (*px).real=33.5; • Note that the parentheses () are necessary initialised with the & (address of operator) just as with any other type • This is such a common operation that there is a special notation for it • e.g. using the struct cplx previously declared (*px).real = px->real • which means take the thing px points to and struct cplx x={1.0, -2.1}, y; struct cplx* px; access its member px=&x; • Pointers allow us to use call-by-reference y=*px; (*px).real=33.5; – useful because it can avoid a lot of copying if structures are large, this can slow down your program 2
Recommend
More recommend