A pointer to a const object of type T must also is const, of type Const t*, meaning that the pointer may is assigned to BU T its contents. const double pi=3.1415926535898; double* p=π Error, would allow *p=4 to change PI const double* p=π OK, can ' t assign to *p (and may be assign to P) double* Const p=π Error, may assign to *p (and not to p) const double* Const p=π OK, both *p and P are const/-----------------------------------a function name used without parenthesis (brackets) is a pointe R to a function. Function pointers can be assigned values and called. int f (double); Functions f and G take double and return int int g (double); int *h (double); function h takes double and returns pointer to int int (*P) (double); P is a pointer to a function that takes double and returns int int main () {p=f; P (3.0); Calls F (3.0) p=g; P (3.0); Calls G (3.0) P=h; Error, type Mismatch/---------------------------------Explicit pointer conversions is allowed but usually unsafe. int I, *p=&i; I=int (3.0); OK, Rounds 3.0 * (double*) p = 3.0; Crash, writes beyond End of I * (double*) &pi = 4; Overwrites a Constthese may also is written (with the same results): i=static_cast<int> (3.0); Apply Standard Conversions *reinterpret_cast<double*>p = 3.0; Pretend P has type double* *CONST_CAST<DOUBLE*>&PI = 4; Same type except for const/----------------------------------the bare name of an array are a const pointer to the first Element. If p is a pointer to an array element, then p+i points I elements ahead, to P[i]. By definition, P[i] is * (p+i). int a[5]; A[0] through a[4] int* p=a+2; *p is a[2] p[1]; A[3] p-a; 2 p>a; True because p-a > 0 p-1 = = a+1//True, both is &a[1] *a; A[0] or p[-2] a=p; Error, A is const (and not *a)/--------------------------------------a literal string enclosed in double quotes are an u nnamed static array of const char with a implied ' as the last element. It May is used either to initialize an array of char, or in an expression as a pointer to the first Char. Special chars in Literals May is escaped with a backslash as before. Literal strings is concatenated without A + operator (convenient to span lines). Char s[]= "ABC"; Char s[4]={' A ', ' B ', ' C ', ' n '}; Const char* p= "A" "b\n"; Points to the ' a ' in the 4 element array "ab\n\0" Const char* answers[2]={"No", "yes"}; Array of pointers to Char cout << answers[1]; Prints Yes (Type const char*) cout << answers[1][0]; Prints y (Type const char) "ABC" [1]//' B ' Arrays don't support copying, assignment, or COmparison. int a[5], b[5]=a; Error:can ' t initialize b this is the b=a; Error:can ' t assign arrays b==a; False, comparing pointers, not contents "abc" = = "abc"//False, comparing pointers to 2 different locationst He size of an array created with new[] May is an expression. The elements cannot is initialized with a list. There is no run time check against accessing deleted elements. int n, *p; CIN >> N; P=new Int[n]; Elements P[0] to p[n-1) with values initially undefined delete[] p; Use Delete with new or new (), delete[] with new[] p[0] = 1; May crash/----------------------------------------static objects is placed in the data segment. They is initialized from values stored in the executable file, and therefore these values must is known at compile time. Initialization occurs only once. Values was maintained when the object was out of scope (e.g. between function calls), and it was safe to return A pointer or reference to them. Numeric values not explicitly initialized is set to 0. int& f () {//Return by reference, F () are an alias for S, not a temporary copy static int s=1;//Initialize D only once ++s; return s; Safe to return by reference} int main () {cout << f (); 2 cout << f (); 3 f () = 5; OK, s=5; s=6; Error, S is not in scope
C + + basic syntax collation