Class Notes (837,549)
Canada (510,313)
CS 137 (16)
Lecture 14

Programming Lecture 14.docx

3 Pages
96 Views
Unlock Document

Department
Computer Science
Course
CS 137
Professor
Andrew Morton
Semester
Fall

Description
Programming Lecture 14 November 6, 2012 Pointers As arguments #include void swap (int *p, int *q) { int temp = *p; *p = *q; *q = temp; } int main () { int i=1, j=2; swap (&I, &j); printf(“%d, %d\n”, i, j); //prints 2, 1 return 0; } As return value // returns pointer to largest array element #include int *largest (int a[], int n) { int m=0; // index of max value for (int i=1; ia[m]) m=1; return &(a[m]); // or return a+m } int main () { int test[] = {0,1,2,3,2,1,0}; int *p = largest (test, sizeof(test)/sizeof(test[0])); *p = 100; printf(“%d\n”, test[3]); return 0; // prints 100 } Pointer arithmetic  Operations o Add an integer to a pointer o Subtract an integer from a pointer o Subtract one pointer from another o EX:  int a[8], *p, *q, i;  p = &a[2];  q = p+3;
More Less

Related notes for CS 137

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit