Wednesday, July 7, 2010

Pointers and Structures

Here is the code for pointers we did in class:

pointerStuff.cpp

#include <iostream>
#include <string> //need this for the structure
using namespace std;

//function prototypes
void SimplePointers();
void ArrayPointers();
void DynamicArray();
void StructurePointer();

int main()
{
//to run one at a time simply
//uncomment it and comment the others
//SimplePointers();
//ArrayPointers();
//DynamicArray();
StructurePointer();
char c;
cin >> c;
}

void SimplePointers()
{
int number=23; //declare a variable

//create a pointer that points to
//the address of that variable
int * pNumber=&number;

//show the number and its address
cout << "number is " << number
<< " the address is " << &number << endl;

//do the same using the pointer
//the pointer stores the address not the value
//using the * "dereferences the pointer
//letting you see the value that is stored
//at that address
cout << "pNumber is " << pNumber
<< " *pNumber is " << *pNumber << endl;

//add 3 to number
number += 3;

//the pointer sees the change in value
cout << " now pNumber is " << pNumber
<< " *pNumber is " << pNumber << endl;

/* pNumber += 2; //this moves the address two integer
//address spaces over
//no longer points to number

cout << " now pNumber is " << pNumber
<< " *pNumber is " << pNumber <<
" number = " << number << endl;*/

*pNumber += 2; //changes number value

cout << " now pNumber is " << pNumber
<< " *pNumber is " << *pNumber <<
" number = " << number << endl;
}

void ArrayPointers()
{
//create a simple array of integers
int myArray[]={2,4,7,3,2};
//create a pointer to the array
//a pointer to an array stores the memory
//address of the first member of the array
int *pArray=myArray;

cout << "*pArray is " << *pArray
<< " the Address of the first element is" << pArray
<< endl;

//move the address over two places
// and then dereference it *(pArray+2)
//to display the third value
cout << "The third element is " << *(pArray+2) << endl;

}

void DynamicArray()
{
/*********************************
a dynamic array declared with the new
keyword allows you to set the size of the
array at run time, somthing that is otherwise
forbidden
************************************/
int size;
cout << "Enter how many elements you want " << endl;
cin >> size;
//dymamically allocate memory to the array
int *pDynamic = new int[size];

//loop through the array entering values
for(int i=0;i < size; i++)
{
cout << "enter a value: ";
cin >> pDynamic[i];
}

cout << "************************" << endl;
//loop through it again displaying
//the stored values
for(int i=0;i < size; i++)
{
cout << pDynamic[i] << endl;
}

//remove the dynamically assigned memory
delete pDynamic;
}




void StructurePointer()
{
//create a simple structure
struct name
{
string firstName;
string mI;
string lastName;
};

//create a dynamic pointer to the structure
name *pName = new name;
//assign it some values
cout << "Enter your first name " ;

//the -> is a membership operator
//firstname is a meber of pName
cin >> pName->firstName;
cout << endl;
cout << "Enter your middle initial " ;
cin >> pName ->mI;
cout << endl;
cout << "Enter your last name" ;
//another way to get to a member element
cin >> (*pName).lastName;
cout << endl;

//output the content of the structure
cout << "Welcome " << pName -> firstName << " "
<< pName ->mI << " " << (*pName).lastName
<< endl;

//delete the dynamically assigned memory
delete pName;

}


Here is the code for the Structure and enum


#include <iostream>
#include <string>
using namespace std;

//create an enum and assign values
enum color
{ red=100, green=300, blue=400};

//create a simple structure
struct product
{
string productName;
double price;
};

int main()
{

//declare and assign values to
//the structure
product book=
{
"Hitchhiker's Guide to the Galaxy",
7.45

};

//output the content of the structure
cout << book.productName << " costs $"
<< book.price << endl;

//declare a new instance of the enum
color band;

//assign a value to the instance
band=blue;

//assign that to an integer
int x = band;

//output the integer
cout << x << endl;

//creat an array of the product structure
product books[4]=
{
{"Zombi apocolypse", 14.95},
{"Neverwhere", 9.98},
{"The Martian Chronicles", 7.98},
{"Ulysses", 15.35}

};

//declare the variable sum and initialize it to 0;
double sum=0;

//use a for loop to loop through the structure
//array showing all its contents
for (int i=0;i<4;i++)
{
cout << books[i].productName << ", "
<< books[i].price << endl;

//total the prices of all the books
sum+=books[i].price;
}

//display the total
cout << "The books total value is " << sum << endl;

char c;
cin >> c;




}

No comments:

Post a Comment