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