


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of pointers and constants in c++. It covers various scenarios, such as assigning and modifying pointers to constants, initializing arrays, and using references. It also explains the concept of a pointer to void and the difference between lvalues and rvalues. An essential resource for students and developers looking to gain a solid understanding of pointers and constants in c++.
Typology: Lecture notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Pointers and constants:
char buffer[100]; char name[20]; const char *p = buffer; // pointer to const char p = name; // ok *p = ’x’; // error! char * const p = buffer; // const pointer to char *p = ’x’; // ok p = name; // error! const char space = ’ ’; const char *p = &space; // ok char *q = &space; // error!
11
Given the following:
char c; const char cc = ’a’; char *pc; const char *pcc; char *const cpc = &c; const char *const cpcc = &cc; char *const *pcpc;
12
which of the following are legal and which ille- gal?
c = cc; cc = c; pcc = &c; pcc = &cc; pc = &c; pc = &cc; pc = pcc; pc = cpc; pc = cpcc; cpc = pc; *cpc = *pc; pc = *pcpc; **pcpc = *pc; *pc = **pcpc;
int a[10]; is equivalent to int* const a = &a[0]; plus (the compiler) allocates 10 words.
int b[10]; b = a; // copy array a into b. // Won’t work, and is illegal.
What if b had been declared as int* b;
A string is an array of characters terminated by ’\0’: char s[5] = “ABCD”; cout << s; char* t; t = s; cout << t;
Arrays Arrays can be initialized: int x[3] = { 5, 6, 7}; x[3] = {3, 4, 5}; // but not assigned like this!
Pointers and arrays Name of an array is a const pointer to first el: int v[4] = { 1, 2, 3, 4}; int* p1 = v; // == int* p1 = &v[0]; int* p2 = &v[0]; (*v)+1 = 10; // but check this one to be sure.
void f(char v[]) { for(int i=0; v[i] != 0; i++) use(v[i]); } is equivalent to: void f(char v[]) { for(char* p=v; p != 0; p++) use(p); }
The result of “++” etc. depends on the type of object pointed to. The compiler takes care of this. 15
Constants const double pi = 3.14; const int v[] = {1, 2, 3, 4}; const int x; // illegal; need to initialize
void g(const X* p) { ... can’t modify *p ... }
void h() { X val; // val can be modified g(&val); // g() still can’t modify val }
consts should always be used in place of “magic numbers”.
16
Pointers and Constants
“Prefixing” a declaration eith const makes the object, not the pointer, a const. To declare the pointer to be constant, use *const.
void f(char* p) { char s[] = ‘‘abcd’’; const char* pc = s; // ptr to const pc[3] = ’e’; // illegal pc = p; //okay
char *const cp = s; // constant pointer cp[3] = ’a’; // ok cp = p; // illegal
const char *const cpc = s; // const ptr to const cpc[3] = ’a’; // illegal cpc = p; // illegal }
Pointers and Constants (contd)
No const* , so const before * applies to the preceding type.
char const cp; // const ptr to char char const pc; // ptr to const char const char* pc2; // ptr to const char
Read left to right.
Pointers,... (Section 6.2)
E.g.: Simple arith. expressions over integers:
struct Exp { int kind; // 1: simple; 2: sum; 3: prod int val; // if kind is 1 Exp* left; // if kind is 2 or 3 Exp* right; // if kind is 2 or 3 };
int Val(Exp* e) { if (1 == e->kind) return(e->val); if (1 == e->kind) return(Val(e->left) + Val(e->right)); if (2 == e->kind) return(Val(e->left) * Val(e->right)); return(-1); }
23
Heap (or “Free store”),... (Section 6.2)
How do we create Exp objects?
Two ways:
Exp e1; // allocated on stack e1.kind = 1; e1.val = 20; e1.left = 0; e1.right = 0;
Exp* sumExp( const Exp* ep1, const Exp* ep2) { Exp* ep = new Exp; // allocated on heap ep->kind = 2; ep->left = ep1; ep->right = ep2; return ep; }
Exp* prodExp( const Exp* ep1, const Exp* ep2){ Exp* ep = new Exp; // allocated on heap ep->kind = 3; ep->left = ep1; ep->right = ep2; return ep; }
24
Heap (contd.)
Exp e1; // allocated on stack e1.kind = 1; e1.val = 20; e1.left = 0; e1.right = 0;
Exp e2; // allocated on stack e2.kind = 1; e2.val = 30; e2.left = 0; e2.right = 0;
Exp* ep1 = sumExp(e1, e2); // on heap Exp* ep2 = prodExp(e1, e2); // on heap Exp* ep3 = sumExp(ep2, ep2); // on heap Exp* ep4 = prodExp(ep1, ep1); // on heap
cout<<"Value of ep4"; printExp(ep4);
Heap (contd.)
printExp void printExp( const Exp* ep) { if( 1==ep->kind) cout<<ep->val; if( 2==ep->kind) { printExp(ep->ep1); cout<<’’ + ’’; printExp(ep->ep2); }
But: Need to “release” the space from the heap, else “memory leaks”.
delete ep1; delete ep2; // etc.
Summary: Stack objects are handled by the system; heap objects must be allocated by call- ing new and released by calling delete.