set02 - Review of Basic Constructs Dr. M. Moussavi 1 C+...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
Dr. M. Moussavi 1 Review of Basic Constructs
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dr. M. Moussavi 2 C++ Data Types Simple Data Types: int, float, char, double, etc. Aggregated Data Types: Arrays and Strings Abstract Data Types: Structures Classes (C++ only) unions Other types Pointers References (C++ only)
Background image of page 2
Dr. M. Moussavi 3 Pointers Draw AR diagram for the following program: #include <iostream.h> int main () { int j, k, *p; p = &j; *p = 3; p = &k; *p = 7; cout << j << endl; cout << *p; return 0; } 3 j AR main 7 k p No arg
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dr. M. Moussavi 4 C++ Reference type Reference is simply an alias (alternative) for a variable name . int main() { int x = 4; int& ref = x; // reference type ref = 18; cout << x; // displays 18 return 0; } 18 x ref AR main Stack No arg
Background image of page 4
Dr. M. Moussavi 5 Another Example int main() { int a , b; int& ref = b; int * ptr = &a; int* & refptr = ptr; *ptr = 4; ref = 8; *refptr = 23 } No Args 23 a 8 b ptr ref AR main rerptr Stack
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dr. M. Moussavi 6 Another Example void modify(int & x) { x++; // point one } int main() { int a = 45; modify (a); } 46 a No arg AR main x AR modify x x is a reference and a is called a referent of x Stack No local
Background image of page 6
Dr. M. Moussavi 7 Reference and Temporary Variable If a numeric constant or expression is passed to a function by reference, a temporary anonymous memory space will be created. This space lives long to make the function call work. See the Following Example:
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dr. M. Moussavi 8 Example int main() { int a = 4; int b = 9; int c = max (a, b-2); } int max(int& x, int& y) { return (x>y)? x : y; } 4 a 9 b ?? c No arg AR main x y 7 Temporary anonymous AR max Stack No local
Background image of page 8
Dr. M. Moussavi 9 Arrays, Pointers, and Pointer Arithmetic
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dr. M. Moussavi 10 Arrays and Pointers The name of an array is treated as a constant pointer that points to the first element of the array. Therefore the array name and pointers of the same type have some similarities: int a [6] = {4,2,3,1,8, 11}; cout << *a; // Using Pointer Notation cout << a[0] // Using Array Notation Both statements above print 4;
Background image of page 10
Dr. M. Moussavi 11 Arrays and Pointers More Examples: int a[6] = {4,2,3,1,8, 11}; int *b; b=a; cout << *b; //displays 4 cout <<*a; // displays 4 cout << b[0] // displays 4 cout << b[1] // displays 2 4 2 3 0 1 2 1000 1004 1008 a b 1 8 11 1012 1016 1020 3 4 5
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dr. M. Moussavi 12 Arrays and Pointers In other words the array notations and pointer notations are interchangeable. What are the the following statements output? cout << *(b+3); cout << a + 2 cout << *(a+1); cout << a; cout << *a + 2; 4 2 3 0 1 2 1000 1004 1008 a b 1 8 11 1012 1016 1020 3 4 5 // Output is 1 // Output is the address of third element // Output is 2 // Output is the address of first element // Output is 6 (4+2)
Background image of page 12
Dr. M. Moussavi 13 Passing Arrays and Strings to Functions Since arrays name and string name are pointers, therefore an array or string can appear as a pointer in a function parameter list. The following function prototypes are equivalent. void fun (int x[]); void fun (int *x); void fun (char string[]); void fun (char *string);
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dr. M. Moussavi 14 Pointer Arithmetic Legal pointer arithmetic in C++ Pointer + Integer Pointer – Integer Pointer – Pointer Pointer++, or ++Pointer Pointer—, or --Pointer Other arithmetic operations are illegal.
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 56

set02 - Review of Basic Constructs Dr. M. Moussavi 1 C+...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online