lecture 13 lecture 13
play

Lecture 13 Lecture 13 2. B inary R epresentation Using Pointers - PDF document

1. Introduction Lecture 13 Lecture 13 2. B inary R epresentation Using Pointers with functions 3. H ardw are and S oftw are 4. H igh Level Languages Last time we saw how to use functions with 5. S tandard input and output


  1. 1. Introduction Lecture 13 Lecture 13 2. B inary R epresentation Using Pointers with functions 3. H ardw are and S oftw are 4. H igh Level Languages • Last time we saw how to use functions with 5. S tandard input and output simple data types 6. Operators, expression and statem ents 7. M aking Decisions • We can also use pointers, the approach is similar 8. Looping e.g. char* tail(char* s, char c); 9. A rrays 10. B asics of pointers returns a identifier argument argument pointer-to-char pointer-to-char char 11. S trings 12. B asics of functions • This takes a string s, looks for char c within it and 13. M ore about functions returns a string starting at that point 14. Files 14. D ata S tructures 16. C ase study: lottery num ber generator /* returns tail of s, from c on */ /* returns tail of s, from c on */ /* Example: passing arrays to functions */ /* returns tail of s, from c on */ /* Example: passing arrays to functions */ /* returns tail of s, from c on */ Computer Architecture"; /* strings may be wrapped using '\' */ • Weve already seen Computer Architecture"; /* strings may be wrapped using '\' */ #include <stdio.h> #include <stdio.h> pointret.c #include <math.h> that arrays are really pointret.c #include <math.h> void print_vec(int v[3]); /* Prints 3-vector v */ void print_vec(int v[3]); /* Prints 3-vector v */ pointers (lecture 9) float length(int v[3]); /* Finds length of 3-vector v */ /* scan along s until c or '\0' is found */ float length(int v[3]); /* Finds length of 3-vector v */ /* scan along s until c or '\0' is found */ /* Example: pointer as return value of functions */ NULL is an appropriate pointer value /* Example: pointer as return value of functions */ NULL is an appropriate pointer value • So we can use a char str[] = "Introduction to Programming and \ void main(void) char str[] = "Introduction to Programming and \ return NULL; /* reached end of s, c not found. void main(void) return NULL; /* reached end of s, c not found. { { similar approach with int a[3] = {2, 4, -6}; int a[3] = {2, 4, -6}; puts("vector a:"); them puts("vector a:"); print_vec(a); /* Often used with arrays and strings */ print_vec(a); /* Often used with arrays and strings */ printf("length = %f\n", length(a)); printf("length = %f\n", length(a)); to signal an error */ to signal an error */ return; puts("Not present in string."); return; puts("Not present in string."); } } • These programs puts("Enter a character:"); void print_vec(int v[3]) /* Prints 3-vector v */ puts("Enter a character:"); char *tail(char *s, char c); illustrate the case void print_vec(int v[3]) /* Prints 3-vector v */ char *tail(char *s, char c); char *tail(char *s, char c) } while (*(t++) != '\0'); { char *tail(char *s, char c) } while (*(t++) != '\0'); { printf("[ %2i ", v[0]); printf("[ %2i ", v[0]); where the array size printf("%2i ", v[1]); printf("%2i ", v[1]); printf("%2i ]\n", v[2]); printf("%2i ]\n", v[2]); p = tail(str, ch); return; is fixed. Later well return; p = tail(str, ch); } #include <stdio.h> } #include <stdio.h> ch = getchar(); ch = getchar(); if (p == NULL) if (*t == c) see how to generalise if (p == NULL) if (*t == c) return t; return t; char ch, *p; char *t = s; float length(int v[3]) /* Finds length of 3-vector v */ char ch, *p; char *t = s; float length(int v[3]) /* Finds length of 3-vector v */ puts(p); the functions for { puts(p); { float len2 = 0; float len2 = 0; int i; variable size arrays int i; main() else main() else for (i = 0; i < 3; i++) for (i = 0; i < 3; i++) do len2 += v[i] * v[i]; do { len2 += v[i] * v[i]; { return sqrt(len2); return sqrt(len2); { } { } } { } { } } call-by-value, call-by-reference call-by-value, call-by-reference • There are two basic ways of passing arguments • Analogies to functions, C allows both – call-by-value • call-by-value • is like taking a photocopy of a document and – the values of the function arguments are copied to the functions giving it to someone. If they change it, the internal parameters. If the copy is changed, the value of the original is unaffected. original argument is not affected. – call-by-reference • call-by-reference • is like giving someone access to the original – the address of the original variables are passed to the function document as pointer arguments. The addresses are copied to the functions internal (pointer) parameters. This the function has full access to the original variables and can change them 1

Recommend


More recommend