# Class 3 CSI2172

## Presentation on theme: "Class 3 CSI2172"— Presentation transcript:

Class 3 CSI2172 http://www.site.uottawa.ca/~fbinard/Teaching/CSI2172_summer

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

Pointeurs and Arrays int a[10]; int * p = a; *p == a[0] p == a == &a[0]

2 dimensions (matrix): double ** alloc_matrix(int n, int m) { double ** M = new double* [n]; int i; for(i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/4112441/slides/slide_4.jpg", "name": "2 dimensions (matrix): double ** alloc_matrix(int n, int m) { double ** M = new double* [n]; int i; for(i=0; i

Deallocating the memory for the matrix void free_matrix(double** M, int n) { int i; for(i = 0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/4112441/slides/slide_5.jpg", "name": "Deallocating the memory for the matrix void free_matrix(double** M, int n) { int i; for(i = 0; i

+ 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

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/4112441/slides/slide_7.jpg", "name": "Function Pointers H You can declare a function pointer.", "description": "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

Class, episode 2 class vector { };