Professional Documents
Culture Documents
Week 3 4
Week 3 4
• Structures
– A collection of simple variables
– Variables can be of different types or heterogeneous
elements
– The data items in a structure are called the members of the
structure
• Structure vs Array
– Structure is collection of heterogenous data elements
– Array is collection of homogenous data elements
• Structure vs Class (comparsion)
– Structure is a collection of data
– Class is a collection of data and functions
Output
Model 6244, part 373, costs $217.55
s1 = Happy
s2 = New Year
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
2003 Prentice Hall, Inc.
Saturday All rights reserved.
Outline
Pointers and Arrays
x[0] x[1] x[2] x[3] x[4] 2003 Prentice Hall, Inc.
All rights reserved.
An example of accessing array
elements using pointer notation
• 1 // arrpointer.cpp
• 2 // Program to print array elements using pointer notation
• 3 #include <iostream>
• 4 #include <conio.h>
• 5
• 6
• 7
• 8 int main()
• 9 {
• 10 int arr[5], *pp, i;
• 11 clrscr();
• 12 pp = arr; // assign each element of an
// array to pointer
variable
• 13 cout<<“Enter values into an array”<<endl;
• 14 for (i=0;i<=4;i++)
• 15 cin>>arr[i];
• 16 cout<<endl<<“Values from array” <<endl;
• 17 for (i=0;i<=4;i++)
• 18 cout<<*pp++<<endl; // increment pointer variable
• 19 getch();
• 20 return 0; // indicates successful termination
• 21 } // end main
Outline
String
ok
1
strings
// Fig. 5.32: fig05_32.cpp fig05_32.cpp
2 // An Array of pointers to strings
output (1 of 1)
3 #include <iostream.h>
4
5
6
7
• const int DAYS = 7; // number of strings in array
9 const int MAX = 10; // maximum size of each string
10 int main()
11 {
12 char star[DAYS][MAX] = {“Sunday”, “Monday”, “Tuesday”,
“Wednesday”, “Thursday”, “Friday”,
“Saturday” }; // array of strings
13 for (int j=0; j<DAYS; j++) // display every string
14 cout << star[j] << endl;
15
16 return 0; // indicates successful termination
17 } // end main
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
2003 Prentice Hall, Inc.
Saturday All rights reserved.
Outline
Another example of string
• 1 // string3.cpp
• 2 // A program to display string with pointer notation
• 3 #include <iostream.h>
• 4 #include <conio.h>
• 5
• 6
• 7
• 8 void dispstr(char*); //prototype
• 9
• 10 int main()
• 11 {
• 12 clrscr();
• 13 char str[] = “Idle people have the least leisure.”;
• 14 dispstr(str);
• 15 return 0; // indicates successful termination
• 15 } // end main
• 16 void dispstr(char* ps)
• 17 {
• 18 cout<<endl; //start on new line
• 19 while (*ps!=‘\0’) //until null character
• 20 cout <<*ps++; //print characters
• 21 }
31
54
77
52
93
2003 Prentice Hall, Inc.
All rights reserved.
Pointers and arrays Outline
(con’t)
• In the previous example, the expression
*(intarray+j)
• We know that the name of an array is its address
• The array starts off with its initial address
(memory location)
• Thus the expression (intarray+j) is thus an address
with some value of “j” is added to it
• For example, the value of “j” is 3, thus the address
of “intarray” plus 3 gives us fourth integer of an
array
• But we want value of this fourth array element not
address, so we use “*” operator, thus gives us 52
2003 Prentice Hall, Inc.
All rights reserved.
47
Another example of pointers and Outline
arrays
fig05_32.cpp
1 // Fig. 5.32: fig05_32.cpp output (1 of 1)
2 // Array accessed with pointers
3 #include <iostream>
4
5
6
7
• int main() {
• int intarray [5] = {31,54,77,52,93}; //array
• int* ptrint; //pointer to int
• ptrint = intarray; //points to intarray
12 for (int j=0; j<5; j++) //for each element
13
14 cout << endl << *(ptrint++); //print value
15
16 return 0; // indicates successful termination
17 } // end main
31
54
77
52
93
2003 Prentice Hall, Inc.
All rights reserved.
Outline
Pointers and arrays (con’t)