Download presentation
Presentation is loading. Please wait.
1
Class 3 CSI2172 http://www.site.uottawa.ca/~fbinard/Teaching/CSI2172_summer
2
Pointeurs (passing by value vs passing by address) void f(int *a) { *a = 2; }... int x = 1; f(&x); //x is 2 void f(int a) { a = 2; }... int x = 1; f(x); //1
3
Pointeurs and Arrays int a[10]; int * p = a; *p == a[0] p == a == &a[0]
4
2 dimensions (matrix): double ** alloc_matrix(int n, int m) { double ** M = new double* [n]; int i; for(i=0; i<n; ++i) M[i] = new double [m]; return M; }
5
Deallocating the memory for the matrix void free_matrix(double** M, int n) { int i; for(i = 0; i<n; ++i) delete M[i]; delete M; }... free_matrix(M, 5); M = 0;
6
+ You can move to the next or previous virtual memory address (calculated based on pointer type) by using the operator + or - pointeur + n n is an integer. pointeur is the name of a variable declared as: type* pointeur; + Compiler interprets as: pointeur + n * sizeof(type) Pointeur arithmetic p[1][2] + 3 == (*(p + 1))[2] + 3 == *(p[1] + 2) + 3 == *(*(p + 1) + 2) + 3
7
Function Pointers H You can declare a function pointer. K int f(); K int (*fp)(); K int *g(); …… int f1(); int f2(); int (*fp)(); fp = &f1; (*fp)(); ……… void array_apply(double*, int, void (*)(double*)); void array_apply(double* a, int n, void (*f)(double*)) { int i; for(i=0; i<n; ++i) (*f)(a+i); } void triple(double *x) { *x *= 3; } void negate(double *x) { *x = - *x; }... double a[10] = {... }; array_apply(a,10,&triple); array_apply(a,10,&negate);
8
Class, episode 2 class vector { };
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.