1 / 22

Chapter 1 Arrays, Pointers, and Structures

Chapter 1 Arrays, Pointers, and Structures. Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2009. Outline. Arrays (first-class arrays, using vector) Strings (using string) Pointers Dynamic allocation Reference variables and parameter passing mechanisms Structures.

pomona
Download Presentation

Chapter 1 Arrays, Pointers, and Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 1 Arrays, Pointers, and Structures Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2009

  2. Outline • Arrays (first-class arrays, using vector) • Strings (using string) • Pointers • Dynamic allocation • Reference variables and parameter passing mechanisms • Structures

  3. 1.1 What are Arrays, Pointers, and Structures? Aggregate: collection of objects stored in one unit Arrays: indexed collections of identical-type objects. (aggregate) Pointers: objects are used to access other objects. 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 always start on 0 Arrays can be used in two different ways: primitive arrays and vectors. Vectors vs. Primitive Arrays Vector has variable size and supports many manipulation functions Primitive Array has fixed size, and support only a few of manipulation functions.

  5. Vectors #include <vector> Declaration: vector<type> identifiers(size); Example: vector<int> a(3); Vector can be indexed as an array, using [ ]

  6. Vector example #include <iostream> #include <vector> using namespace std; int main() { vector<int> aa(3); cout<<"Hellow World!!!!!!!"<<endl; aa[0]=0; aa[1]=1; aa[2]=2; for(int i=0; i<aa.size(); i++) cout<<aa[i]<<endl; char myLine[100]; cin.getline(myLine,100); }

  7. Vectors Vector 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 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

  8. How to do Vector’s resize Example: vector<int> arr(10); arr.resize(12);

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

  10. Multidimentional Arrays • A multidimentional array is an array that is accessed by more than one index. • An example to declare a 2D array: int aa[3][3]

  11. 2D array example #include <iostream> using namespace std; int main() { int num_i=3, num_j=3; int aa[num_i][num_j]; aa[0][0]=10; aa[0][1]=10; aa[0][2]=10; aa[1][0]=10; aa[1][1]=10; aa[1][2]=10; aa[2][0]=10; aa[2][1]=10; aa[2][2]=10; for(int i=0; i<3; i++) for(int j=0; j<3; j++) cout<<aa[i][j]<<endl; char myLine[100]; cin.getline(myLine,100); }

  12. Pointers • Declare a pointer int *ptr; • & : referencing operator that returns the address of the object. • * : dereferencing operator which can access data through a pointer

  13. Pointers int *ptr; int x = 5; int y = 7; We can make ptr point at x by assigning to ptr the memory location where x is stored ptr = &x

  14. Pointers • Or we can just simply assign a value to ptr by using dereferencing operator *: *ptr = 10;

  15. To help you memory • If we define int *ptr; Then ptr itself is an address, therefore, if ptr = “something”; this “something” must be an address

  16. To help you memory • If we define int *ptr; Then *ptr itself is a value, therefore, if *ptr = “something”; this “something” must be a value

  17. Pointers • Of course, we can have something like (but not meaningful) int *ptr = &x;

  18. Pointer Example #include <iostream> using namespace std; int main() { int x=5, y=7; int *ptr; ptr=&y; cout << *ptr << endl; char myLine[100]; cin.getline(myLine,100); }

  19. Pointer Example #include <iostream> using namespace std; int main() { int x=5, y=7; int *ptr; ptr=&y; cout << *ptr << endl; cout << ptr << endl; *ptr+=1; cout << *ptr << endl; cout << ptr << endl; *ptr++; cout << *ptr << endl; cout << ptr << endl; char myLine[100]; cin.getline(myLine,100); }

  20. Structures Stores a collection of objects that need not be of the same type. Each object in the structure is a member and is accessed by applying the dot (.) member operator, not index as in array. In C, structures are used quite often. But in C++, they are replaced by classes at most cases.

  21. Structures Struct Student{ string firstName; string lastName; … }; Student a; // declaring Student a.firstName = “Mary”;//accessing member data

  22. Linked List struct Node { int value; Node *next };

More Related