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

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

Embed Size (px)

Citation preview

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

Chapter 1Arrays, Pointers, and Structures

Saurav KarmakarSpring 2007

Page 2: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

Syntax vs. Semantics

Syntax: the form of the presentation

Semantics: the meaning of the

presention

Page 3: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

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)

Page 4: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

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)

Page 5: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

First class object (vectors) vs. second class object (arrays)

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.

Page 6: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

Vectors

#include <vector> Declaration:

vector<type>identifiers(size); Example: vector<int> a(3);

Vector can be indexed as an array, using [ ]

Page 7: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

VectorsVector member functions:

vector<int> 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

Page 8: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

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

Page 9: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

How to do Vector’s resize

Example:

vector<int> arr(10); arr.resize(12);

Page 10: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

Function calls Call by value: The actual argument

is copied into the parameter ex: int findMax(vector<int> a);

Call by reference: avoids copy, it allows change of the parameter

int findMax(vector<int> &a);

Page 11: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

Function calls

Call by constant reference: avoids copy and guarantees that actual parameter will not be change

ex. int findMax(const vector<int> &a);

Page 12: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

Multidimensional Array

Second class object

First class equivalent is matrix

Syntax : matrix<int> x(2,3);

Page 13: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

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

result:“o” cout<< s <<endl;

Page 14: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

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

Page 15: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

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

Page 16: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

Legal Pointer Syntax

int x=10Declare a pointer: int *ptr=&x or int *ptr ptr=&x After declare: *ptr=15

Page 17: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

Illegal Pointer Syntax

int *ptr //run time error

*ptr=&x

ptr=x or int *ptr=x

Page 18: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

Pointer

*ptr = x // Symantically incorrect

What happens bellow ? int x=5; int *ptr = &x; *ptr +=1; *ptr++;

Page 19: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

Pointers

What happens bellow ? type *ptr1, *ptr2; ptr1=ptr2; *ptr1=*ptr2;

C++ is strongly typed language.

Page 20: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

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”

Page 21: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

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

cout<< (*str_ptr).length() <<endl; //5

delete str_ptr;

Page 22: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

Stale pointer… double delete

string *s = new string(“hello”);

string *t=s;

delete t;

Page 23: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

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

Page 24: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

Cont.#include<iostream>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=7swap_c(&x,&y); // x=7, y=5swap_ref(x,y); // x=5, y=7

}

Page 25: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

Structures

Structures: collections of objects that need not be of the same type.

Syntax: struct identifier { };

Page 26: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

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

Page 27: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

Structure Data…

Indigenous data are completely contained by the structure.

Exogenous data reside outside the structure and are accessed through a pointer.

Page 28: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

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.

Page 29: Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007

Summary

•Vectors vs. arrays•First class objects vs. second class

objects•Ways to pass data to functions•Pointers•Dynamic memory allocation &

deallocation•“*”, “&” , “.” , “->”