Chapter 1 Arrays, Pointers, and Structures - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Chapter 1 Arrays, Pointers, and Structures

Description:

First class object (vectors, string class) : can be manipulate in all the usual ways. ... v.front() // return the first element in v. v.back() // return the ... – PowerPoint PPT presentation

Number of Views:151
Avg rating:3.0/5.0
Slides: 30
Provided by: csg3
Category:

less

Transcript and Presenter's Notes

Title: Chapter 1 Arrays, Pointers, and Structures


1
Chapter 1Arrays, 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) 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.

6
Vectors
  • include ltvectorgt
  • Declaration vectorlttypegtidentifiers(size)
  • Example vectorltintgt a(3)
  • Vector can be indexed as an array, using

7
Vectors
  • Vector member functions
  • vectorltintgt 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 Vectors resize
  • Example
  • vectorltintgt arr(10)
  • arr.resize(12)

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

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

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

13
Strings
  • includeltstringgt
  • string sHello World!
  • Int sizes.length()
  • coutltlt s4 ltltendl // resulto
  • coutltlt s ltltendl

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 x5
  • int ptr
  • ptrx
  • cout ltlt ptr ltlt endl // output 0013FF7C

15
Pointer cont.
  • unary deferencing operator which can access
    data/object being pointed.
  • ptr 10
  • Example
  • int x5
  • int ptrx //ptr5 ptr 0013FF7C
  • ptr10 //ptrx10
  • Illegal ptrx //x is not an address

16
Legal Pointer Syntax
  • int x10
  • Declare a pointer
  • int ptrx or int ptr
  • ptrx
  • After declare
  • ptr15

17
Illegal Pointer Syntax
  • int ptr //run time error
  • ptrx
  • ptrx or int ptrx

18
Pointer
  • ptr x // Symantically incorrect
  • What happens bellow ?
  • int x5
  • int ptr x
  • ptr 1
  • ptr

19
Pointers
  • What happens bellow ?
  • type ptr1, ptr2
  • ptr1ptr2
  • ptr1ptr2
  • 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")
  • coutltlt str_ptr ltlt endl //Hellow
  • coutltlt (str_ptr).length() ltltendl //5
  • delete str_ptr

22
Stale pointer double delete
  • string s new string(hello)
  • string ts
  • 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 l0
  • int c l
  • c s // Not allowed

24
Cont.
  • includeltiostreamgt
  • using namespace std
  • void swap_wrong (int a, int b)
  • int tempa ab btemp
  • void swap_c (int a, int b)
  • int tempa ab btemp
  • void swap_ref (int a, int b)
  • int tempa ab btemp
  • void main()
  • int x5,y7
  • swap_wrong(x,y) // x5, y7
  • swap_c(x,y) // x7, y5
  • swap_ref(x,y) // x5, y7

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-gtlastName 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
  • , , . , -gt
Write a Comment
User Comments (0)
About PowerShow.com