Define the following and provide an example of each: * Pointer * Arrays
Answer the following questions and provide an example of each:
What is the difference between a one-dimensional and a two dimensional arrays?
Arrays store items that have the same type of data type like a group of employees’ names and social security numbers for a team of 2000 personal. Pointer is a variable that greatly extends the power and flexibility of a program, each memory location that is used to store data value has an address. The address provides the means for a PC hardware to reference a particular data item.
Ex. of an Array
// Ex4_02.cpp
// Demonstrating array initialization
#include < iostream >
#include < iomanip > using std::cout; using std::endl;
Available for download on
Wrox.com
Copyright © 2010 John Wiley & Sons, Inc. using std::setw; int main()
{
int value[5] = { 1, 2, 3 }; int junk [5]; cout < < endl; for(int i = 0; i < 5; i++) cout < < setw(12) < < value[i]; cout < < endl; for(int i = 0; i < 5; i++) cout < < setw(12) < < junk[i]; cout < < endl; return 0;
}
Ex. of a Pointer
// Ex4_05.cpp
// Exercising pointers
#include < iostream > using std::cout; using std::endl; using std::hex; using std::dec; int main()
{
long* pnumber(nullptr); // Pointer declaration & initialization long number1(55), number2(99); pnumber = & number1; // Store address in pointer
*pnumber += 11; // Increment number1 by 11 cout < < endl
< < "number1 = " < < number1
< < " & number1 = " < < hex < < pnumber; pnumber = & number2; // Change pointer to address of number2 number1 = *pnumber*10; // 10 times number2 cout < < endl
< < "number1 = " < < dec < < number1
< < " pnumber = " < < hex < < pnumber
Available for download on
Wrox.com
NOTE The