lecture12

# lecture12 - Last time Introduction to Low-Level Programming...

This preview shows pages 1–4. Sign up to view the full content.

Introduction to Low-Level Programming Concepts CMSC 212 Lecture 12 1 CMSC 212 October 8, 2009 Last time Chapter 6, Pointers the difference between pointers and arrays arrays of pointers – generic ( void ) pointers types of pointers to multidimensional arrays Chapter 7, Functions 2 CMSC 212 definitions, arguments function arguments more about prototypes Reading Read Chapter 11 in Reek (Pointers on C) next 3 CMSC 212 Chapter 7, Functions, con't. 4 CMSC 212

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Function example int power(int base, int exp); int power(int base, int exp) { int ans= 1; while (exp > 0) { ans *= base; exp--; } 5 CMSC 212 return ans; } int main() { int n= 2, m= 3; printf("%d\n", power(n, m)); printf("%d %d\n", n, m); return 0; } Recursive functions An example of a directly recursive function: void print_binary(unsigned int n); void print_binary(unsigned int n) { if (n > 1) print_binary(n >> 1); if (n % 2 == 0) 6 CMSC 212 The recursion can be indirect, for example a() calls b() which calls a() , etc. printf("0"); else printf("1"); } Passing arrays Recall that an entire array name (without any subscripts) is treated as a pointer type variable it points to the array's first element (the one with subscript 0) an array argument is passed to a function as the address of its first element, so modifications a function makes to elements of an array are seen by the calling function #include <stdio.h> void swap_first_two_elements(int a[]) { 7 CMSC 212 int temp= a[0]; a[0]= a[1]; a[1]= temp; } int main() { int x[2]= {5, 23}; swap_first_two_elements(x); return 0; } Passing and returning arrays Another example function with an array parameter: int find(const int data[], int size, int value); int find(const int data[], int size, int value) { int i; for (i= 0; i < size; i++) 8 CMSC 212 What happens if a function returns an array? if (data[i] == value) return i; return -1; }
Another function with an array parameter void shift(int arr[], int *const size); void shift(int arr[], int *const size) { int i; (*size)--; 9 CMSC 212 for (i= 0; i < *size; i++) arr[i]= arr[i + 1]; } Passing multidimensional arrays All arrays are passed as the base address of their first element, but the compiler needs help to know what a multidimensional array's dimensions are Example: int matrix[3][10]; func(matrix); /* want to pass to a function f() */ 10 CMSC 212 these notations for the parameter are equivalent- the first says that mat is a two-dimensional array with 10 columns, the second says it's a one-dimensional array of pointers to arrays of 10 int s void f(int mat[][10]); /* array of 10-elt arrays */ void f(int (*mat)[10]); /* same */ Passing multidimensional arrays, con't. The sizes of all but the first (leftmost) dimension of a multidimensional array parameter must be specified. The size of the first dimension (even for one-dimensional arrays) is ignored even if it's given and any array can be passed into the parameter if it has the same element type, provided that the sizes of all its dimensions other than the first one are the same as those 11 CMSC 212 of the parameter Array and pointer parameters Array and pointer notation for parameters are interchangeable #include <stdio.h> void f(const int a[]) { printf("%d %d\n", a[0], *a); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern