11-Midterm_Review_2spp

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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
Parameter Passing in C 3 Pointer Arithmetic 4
Background image of page 2
Pointer Arithmetic 5 Pointer Arithmetic 6
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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.
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
Static Local Variables 13 Dynamic Memory Management 14
Background image of page 7

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

View Full DocumentRight Arrow Icon
Exercise 15 int main () { int x = 10; int *p, *q; q = (int *) malloc(sizeof (int));
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 24

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

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

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