140 likes | 240 Vues
This comprehensive guide introduces pointers in programming, a fundamental concept that allows developers to directly access and manipulate memory locations. It covers various datatype pointers (int, char, bool, double) and explains how to use them effectively in C++. Key topics include defining pointers, manipulating memory addresses, pointer arithmetic, and passing arrays by reference. With practical examples and clear explanations, this resource is essential for anyone looking to enhance their programming skills and deepen their understanding of memory management in software development.
E N D
A Pointer: an address, a reference, a location of the computer memory A pointer of what? int, char, bool, double, or any kind of data type Can be need to know so the computer knows how to read or store the contents of the memory.
Define pointers // define pointers int a, *ap; // can’t use a and *a at the same time; double b, *bp; token k, *kp; a is an integer, ap is a pointer to an integer; b is a double, bp is a pointer to a double; k is a token, kps a pointer to a token.
A Pointer points to a location of the computer memory } 4 bytes FFFFA080 FFFFA084 a ap b bp k kp 5 int a=5; int *ap; double b=2.3; double *bp; token k; token *kp; bp = new double; *bp = 3.14; *ap = 7; // not new-ed yet ? 2.3 1 3.14 1 ?
A Pointer points to a location of the computer memory } 4 bytes FFFFA080 FFFFA084 a ap b c 5.1 double a=5.1; double *ap; double *b,*c; ap = &a; // &a :the address of a *ap=1.7; b = new double; *b = 3.14; b = &a; c = b; 1.7 3 1 4 1 new 2 2 3.14 3 4
The name of an array is a pointer } 4 bytes a p int a[4]={5,60,700,8000}; int *p; p=a; cout << *p << endl; cout << *p+1 << endl; cout << *(p+1) << endl; cout << *(p+2) << endl; cout << *(p+3) << endl; cout << *(p+4) << endl; 1 1 2 2 FFFFA080 FFFFA084 5 6 60 700 8000 #$&@
Call by value double fun(int n, double p) { for (int i=0; i < 2; i++) p = p-n; return p; } n p i 113.4 118.4 2 1 1+4=5 i k p s 123.4 int main() { int i=1,k=4; double p,s=123.4; p = fun(i+k,s); cout << i << “\n“ << k << “\n“ << p << “\n“ << s; } 1 4 113.4 123.4
Call by reference double fun(int n, double & p) { for (int i=0;i < 2;i++) p = p-n; return p; } n p i 1 2 1+4=5 &s i k p s int main() { int i=1,k=4; double p,s=123.4; p = fun(i+k,s); cout << i << “\n“ << k << “\n“ << p << “\n“ << s; } 1 4 113.4 113.4 118.4 113.4
Call by Value vs. Call by Reference int add_one_V(int n) { n++; cout << “1:“ << n << endl; return n; } n int sub_one_R(int &n) { n--; return n; } n 2 1 1:6 2:6 5 3:4 4 int main() { int a,b=5; a = add_one_V(b); cout << “2:“ << a << “ “ << b << endl; a = sub_one_R(b); cout << “3:“ << a << “ “ << b << endl; } &b a b 1 3 2 3
Passing Arrays as Parameters int top(int a[]) { int i=0; return a[i]; } a i int third(int a[]) { int i=2; return a[i]; } a i int main() { int a[4]={10,200,300,400}; cout << top(a) << endl; cout << third(a) << endl; } a a+2 10 300
Arrays passed as References void fun(int a[]) { int i=2; a[i] += 5; } a i int main() { int a[4]={10,200,300,400}; fun(a); cout << a[0] << “ “; cout << a[1] << “ “; cout << a[2] << “ “; cout << a[3] << endl; return 1; } a a[2] 10 200 305 400
Pass the size of an Array void add_one_to_all(int A[], int size) { for (int i=0;i<size;i++) A[i]++; } i A size int main() { const int size=4; int a[size]={1,2,3,4}; add_one_to_all(a,size); for (int i=0;i<size;i++) cout << a[i] << “ “; return 1; } size i a 2 3 4 5
Vector passed as Value void add_one_to_all(vector<int> v) { for (int i=0;i<v.size();i++) v[i]++; } i v int main() { vector<int> a(4); int i; for (i=0;i<a.size();i++) a[i]=i+1; add_one_to_all(a); for (i=0;i<a.size();i++) cout << a[i] << “ “; } i a 1 2 3 4
Vector passed as Reference void add_one_to_all(vector<int> &v) { for (int i=0;i<a.size();i++) a[i]++; } i v int main() { vector<int> a(4); for (int i=0;i<a.size();i++) a[i]=i+1; add_one_to_all(a); for (int i=0;i<a.size();i++) cout << a[i] << “ “; } &a i a 2 3 4 5
C-string is an array of char } 4 bytes a b c d 1 byte } char *a = "Dennis"; char b[7] = {'D','e','n', 'n','i','s','\0'}; char *c=b; cout << *a << “ “ << b[0] << endl; cout << *(a+2) << “ “ << b[2] << endl; cout << a << “ “ << b << endl; char *d[2] = {c, "Li"}; cout << d[0] << " " << d[1] << endl;; 1 1 d[0] d[1] D D n n Dennis Dennis Dennis Li