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

5
Deallocating the memory for the matrix void free_matrix(double** M, int n) { int i; for(i = 0; i

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

8
Class, episode 2 class vector { };

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google