void Pointers to functions Pointers to Functions u
- Slides: 26
void * Pointers to functions
Pointers to Functions u. C/C++ allow to have a pointer to a function int foo(int x) {…} main() { int (*func)(int); // func is a pointer to a function func = &foo; func = foo; // same int x = (*func)(7); x = func(7) //same! } 4
example: Numerical Integrator 6
example: Numerical Integrator double numerical. Integration( double a, double b, double (*func)(double), int k ) { double delta = (b - a)/k; double Sum = 0; for( double x = a+0. 5*delta; x < b; x+= delta ) Sum += (*func)(x) * delta; return Sum; } 7
Typedef typedef int my. Int, shalem; typedef int i_arr 1[15], i_arr 2[9][6]; typedef char my. Char, tav, *char. Pnt, char_arr 1[100]; /* now declare some objects */ /* ints */ my. Int x 1; shalem x 2; i_arr 1 y 1; /* array of 15 ints */ i_arr 2 y 2; /* 9*6 array of int */ tav ch; /* a char */ char. Pnt pnt; /* pointer to char */ char_arr y 3; /* array of 100 char */ 10
' למצביעים לפונ typedef int(*PCompare. Func) (const int, const int); . . . PCompare. Func comp= &int_compare; 12
typedef שימוש מדורג ב typedef int dim 1_arr[2]; typedef dim 1_arr dim 2_arr[2]; typedef dim 2_arr dim 3_arr[2]; int main() { //now can be defined: dim 3_arr array. Using. Typedef = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}; //same as: int usual. Array[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}; } 13
automatic cast char a = 10; int b = 5; b = a; // automatic cast from char to int occures // also called “upcast” a = b; // Compiler will complain. Explicit cast needed אך , מהפרטי לכללי מבוצעת המרה אוטומטית : העקרון לא בכוון ההפוך 17
void *p defines a pointer to undetermined type int i; char ch; int* p. Int = &j; char* p. Ch = &ch; void* q; q = p. Int; // no cast needed q = p. Ch; // no cast needed p = (int*)q ; // cast is needed Looks good? But: 18
void * cannot: u. We cannot access the content of the pointer! int j; void *p int k = = &j; *p; // illegal (int)*p ; // still illegal *(int*)p; // legal 19
Pointer arithmetic - Reminder int a[4]; int *p 1 = &a[0]; int *p 2 = &a[3]; p++; // then, p == &a[1] int dif = p 2 – p 1; // == (3 – 0) -------------------------With void*, the above is impossible too! 20
So, what is it good for? 21
Example: qsort Library procedure: qsort( void *base, int n, int size, int (*compare)(void const*, void const*) ); – beginning of an array u n – number of elements u size – size of each element u compare – comparison function u base 22
- Void main(void)
- Void main(void)
- Void loop(void)
- Pointers and strings
- Advanced pointers in c
- Pointers are variables that contain as their values
- Exmple
- Hazard pointers
- Pointers symbol
- Pointers are variables that contain
- Positional index information retrieval
- Science fair logbook table of contents
- Java pointers
- Java pointers and references
- Converging pointers algorithm
- C array of pointers to structs
- 12 pointers
- Which is a good idea for using skip pointers
- Skip pointers in information retrieval
- & vs * in c
- Pointers are variables that contain
- What is inter function communication in c
- File pointers in c
- Advanced pointers in c
- Pointers basics
- I-need-a-few-pointers-98hpou5
- Explicit pointer