Lesson 08 ELET 2300 Arrays and Pointers Fall 2014 Sample Programs.docx - Lesson 08 ARRAYS and Pointers Sample Programs\/SP8_1.cpp This program uses the

# Lesson 08 ELET 2300 Arrays and Pointers Fall 2014 Sample Programs.docx

This preview shows page 1 - 5 out of 29 pages.

Lesson 08 – ARRAYS and Pointers Sample Programs //SP8_1.cpp // This program uses the & operator to determine a variable’s // address and the sizeof operator to determine its size. #include <iostream> using namespace std; int main() { int x = 15; cout << "The address of x is " << &x << endl; //always in hex notation cout << "The size of x is " << sizeof (x) << " bytes\n" ; cout << "The value in x is " << x << endl; return 0; } /* The address of x is 0014F75C The size of x is 4 bytes The value in x is 15 */ 1
Lesson 08 – ARRAYS and Pointers Sample Programs //SP8_2.cpp // This program stores the address of a variable in a pointer. #include <iostream> using namespace std; int main() { int x = 15; // int variable int *ptr; // Pointer variable declaration, can point to an int ptr = &x; // Store the address of x in ptr (pointer initialization) cout << "The value in x is " << x << endl; cout << "The address of x is " << &x << endl; cout << "The address of x via pointer is " << ptr << endl; cout << "The value in x derefenced via pointer is " << *ptr << endl; return 0; } /* The value in x is 15 The address of x is 0048F9F8 The address of x via pointer is 0048F9F8 The value in x dereferenced via pointer is 15 */ 2
Lesson 08 – ARRAYS and Pointers Sample Programs //SP8_3.cpp // This program demonstrates the use of the indirection operator. #include <iostream> using namespace std; int main() { int x = 15; // int variable int *ptr; // Pointer variable declared, can point to an int ptr = &x; // Pointer initialization. Store the address of x in ptr //Address of x, address of ptr, and value of ptr cout << "Address of x is " << &x << endl; cout << "Address of ptr is " << &ptr << endl; cout << "Value of ptr (the address of x) is " << ptr << endl; // Use both x and ptr to display the value in x. cout << "Here is the value in x, printed twice:\n" ; cout << "The value of x is " << x << endl; // Displays the contents of x cout << "The value of x using *ptr is " << *ptr << endl; // Displays the contents of x // Assign 230 to the location pointed to by ptr. // This will actually assign 230 to x. *ptr = 230; // Use both x and ptr to display the value in x. cout << "Once again, here is the value in x:\n" ; cout << "The value of x is " << x << endl; // Displays the contents of x cout << "The value of x using *ptr is " << *ptr << endl; // Displays the contents of x return 0; } /* Address of x is 003CF998 Address of ptr is 003CF98C Value of ptr (the address of x) is 003CF998 Here is the value in x, printed twice: The value of x is 15 The value of x using *ptr is 15 Once again, here is the value in x: The value of x is 230 The value of x using *ptr is 230 */ 3
Lesson 08 – ARRAYS and Pointers Sample Programs //SP8_4.cpp // This program demonstrates a pointer variable referencing // different variables. #include <iostream> using namespace std; int main() { int x = 20, y = 50, z = 80; // Three int variables declared and initialized int *ptr; // Pointer variable declaration // Display the contents of x, y, and z. cout << "Here are the values of x, y, and z:\n" ; cout << "x = " << x << "\ty = " << y << "\tz = " << z << endl; // Display address of x, y, and z. cout << "Address of x = " << &x << "\tAddress of y = " << &y << "\tAddress of z = " << &z << endl; // Use the pointer to manipulate x, y, and z.

#### You've reached the end of your free preview.

Want to read all 29 pages?

• Fall '15
• Marinolent