11-Midterm_Review_2spp

# 11-Midterm_Review_2spp - CSC 4304 Systems Programming Fall...

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

1 CSC 4304 - Systems Programming Fall 2010 Tevfik Ko ! ar Louisiana State University October 7 th , 2010 Lecture - XI Midterm Review Parameter Passing in C 2

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

View Full Document
Parameter Passing in C 3 Pointer Arithmetic 4
Pointer Arithmetic 5 Pointer Arithmetic 6

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

View Full Document
Exercise int main () { int i, r[6] ={1,1,1,0,0,0}; int *ptr; ptr = r; *ptr = 10; *(ptr +1) = 5; r[2] = *ptr; *(ptr++)=20; ptr+=2; *(++ptr)=20; for (i=0; i < 6; i++) printf (" r[%d] = %d\n", i, r[i]); } 7 Function Pointers • Functions are not variables but we can define pointers to functions which will allow us to manipulate functions like variables. . • int f() : a function which returns an integer • int* f() : a function which returns a pointer to integer • int (*f)(): a pointer to a function which returns integer • int (*f[])(): an array of pointer to a function which returns integer 8
Example void sum( int a, ! int b) {printf(“sum: %d\n”, a+b);} void dif( int a, ! int b) {printf(“dif: %d\n”, a-b);} void mul( int a, ! int b) {printf(“mul: %d\n”, a*b);} void div( int a, ! int b) {printf(“div: %f\n”, a/b);} void (*p[4]) ! ( int x, ! int y); int main( void ) { !! int result; !! int i=10, ! j=5, ! op; !! p[0] ! = ! sum; ! /* ! address ! of ! sum() ! */ !! p[1] ! = ! dif; ! /* ! address ! of ! dif() ! */ !! p[2] ! = ! mul; ! /* ! address ! of ! mul() ! */ !! p[3] ! = ! div; ! /* ! address ! of ! div() ! */ for (op=0;op<4;op++) (*p[op]) ! (i, ! j); } 9 Operator Precedence 10 () [] -> . left to right primary expr.

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

View Full Document
Exercise 1. int *a[] : 2. int (*a)[] : 3. int* (*a)() : 4. int* ((a())[])() : 5. int (*(*a())[])() : 6. int* (*(*a[])())[] : 11 Solutions int *a[] : array[] of pointer to int int (*a)[] : pointer to array[] of int int* (*a)() : pointer to function which returns pointer to int int* ((a())[])() : function which returns array[] of functions that return pointer to int int (*(*a())[])() : function which returns pointer to array of pointers to functions which return pointer to int int* (*(*a[])())[] : array of pointer to function which returns pointer to array of pointer to int 12
Static Local Variables 13 Dynamic Memory Management 14

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

View Full Document
Exercise 15 int main () { int x = 10; int *p, *q; q = (int *) malloc(sizeof (int));
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 11/23/2011 for the course CSC 4303 taught by Professor Staff during the Fall '10 term at LSU.

### Page1 / 24

11-Midterm_Review_2spp - CSC 4304 Systems Programming Fall...

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

View Full Document
Ask a homework question - tutors are online