lecture12 - Introduction to Low-Level Programming Concepts...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Low-Level Programming Concepts CMSC 212 ecture 12 1 CMSC 212 Lecture 12 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 hapter 7, Functions 2 CMSC 212 Chapter 7, Functions 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 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); 6 CMSC 212 The recursion can be indirect, for example a() calls b() which calls a() , etc. if (n % 2 == 0) 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> 7 CMSC 212 void swap_first_two_elements(int a) { 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....
View Full Document

This note was uploaded on 11/23/2009 for the course CMSC 212 taught by Professor Staff during the Spring '08 term at Maryland.

Page1 / 13

lecture12 - Introduction to Low-Level Programming Concepts...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online