Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007.

Similar presentations


Presentation on theme: "Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007."— Presentation transcript:

1 Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

2 Syntax vs. Semantics Syntax: the form of the presentation Semantics: the meaning of the presention

3 1.1 What are Arrays, Pointers, and Structures Pointers: store an address. Used to access objects Aggregate: collection of objects stored in one unit. Arrays: indexed collections of identical-type objects. (aggregate) Structures: collections of objects that need not be of the same type. (aggregate)

4 1.2 Arrays and Strings Arrays: indexed collections of identical- type objects. Array index always start on 0 Arrays can be used in two different ways: primitive arrays and vectors. First class object (vectors) vs. second class object (arrays)

5 First class object (vectors, string class) : can be manipulate in all the usual ways. second class object (arrays) : can be manipulate in only certain restricted ways.

6 Vectors #include Declaration: vector identifiers(size); Example: vector a(3); Vector can be indexed as an array, using [ ]

7 Vectors Vector member functions: vector v(10); v.at() // equal to v[ ] v.size() // return the number of elements in v v.front() // return the first element in v v.back() // return the last element in v v.clear() // delete all the element in v v.empty() // return 1 if v is empty; else return 0

8 Vectors v.resize( val ) // change size of v to val v.pop_back() // remove the last element in v v.push_back( val ) // appends val to the end of v v.capacity() // return the number of element that vector can hold before it will need to allocate more space http://www.cppreference.com/cppvector/all.html

9 How to do Vector’s resize Example: vector arr(10); arr.resize(12);

10 Function calls Call by value: The actual argument is copied into the parameter ex: int findMax(vector a); Call by reference: avoids copy, it allows change of the parameter int findMax(vector &a);

11 Function calls Call by constant reference: avoids copy and guarantees that actual parameter will not be change ex. int findMax(const vector &a);

12 Multidimensional Array Second class object First class equivalent is matrix Syntax : matrix x(2,3);

13 Strings #include string s=“Hello World!”; Int size=s.length(); cout<< s[4] <<endl; // result:“o” cout<< s <<endl;

14 1.3 Pointers syntax in C++ How to Declare a pointer int *ptr; & : unary operator that returns the address of the object, it is placed before. int x=5; int *ptr; ptr=&x; cout << ptr << endl; // output: 0013FF7C

15 Pointer cont. * : unary deferencing operator which can access data/object being pointed. *ptr = 10; Example: int x=5; int *ptr=&x; //*ptr=5 ptr= 0013FF7C *ptr=10 //*ptr=x=10 Illegal: ptr=x //x is not an address

16 Legal Pointer Syntax int x=10 Declare a pointer: int *ptr=&x or int *ptr ptr=&x After declare: *ptr=15

17 Illegal Pointer Syntax int *ptr //run time error *ptr=&x ptr=x or int *ptr=x

18 Pointer *ptr = x // Symantically incorrect What happens bellow ? int x=5; int *ptr = &x; *ptr +=1; *ptr++;

19 Pointers What happens bellow ? type *ptr1, *ptr2; ptr1=ptr2; *ptr1=*ptr2; C++ is strongly typed language.

20 1.4 Dynamic memory management new operator allocates memory and returns a pointer to the newly created object. When an object, that is allocated by new, is no longer referenced, an operator delete must be applied to the object, through its pointer, to avoid “memory leakage”

21 Cont. Example: string *str_ptr; str_ptr = new string("Hello"); cout<< *str_ptr << endl; //Hellow cout<< (*str_ptr).length() <<endl; //5 delete str_ptr;

22 Stale pointer… double delete string *s = new string(“hello”); string *t=s; delete t;

23 1.5 Reference variables Reference type is an alias and may be viewed as a pointer constant that is always dereferenced implicitly. Reference variables must be initialized at declaration time. Viz.int l=0; int &c = l; c = s; // Not allowed

24 Cont. #include using namespace std; void swap_wrong (int a, int b) {int temp=a; a=b; b=temp;} void swap_c (int *a, int *b) {int temp=*a; *a=*b; *b=temp;} void swap_ref (int &a, int &b) {int temp=a; a=b; b=temp;} void main() { int x=5,y=7; swap_wrong(x,y); // x=5, y=7 swap_c(&x,&y); // x=7, y=5 swap_ref(x,y); // x=5, y=7 }

25 Structures Structures: collections of objects that need not be of the same type. Syntax: struct identifier { };

26 Structures Struct Student { string firstName; string lastName; … }; Student a, *sPtr; // declaring Student a.firstName= “Mary”;//modifying member data sPtr= &a; (*sPtr).lastName= “Smith”; sPtr->lastName= “Smith”; // same as above

27 Structure Data… Indigenous data are completely contained by the structure. Exogenous data reside outside the structure and are accessed through a pointer.

28 Copying objects Shallow copy: a copy of pointers rather than data being pointed at Deep copy: a copy of data being pointed at rather than the pointers.

29 Summary Vectors vs. arrays First class objects vs. second class objects Ways to pass data to functions Pointers Dynamic memory allocation & deallocation “*”, “&”, “.”, “->”


Download ppt "Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007."

Similar presentations


Ads by Google