// ***************************************************************** // Author: Zhixiang Chen // Class: CSCI/CMPE 2380, Spring 2009 // Lab 1: Pointers, pointer arithmetic, pointers vs. arrays, // pointer parameters // Date: January 13, 2009 // Comment: The code here is meant to be revised. // //----------------------------------------------------------------- // // This lab exercise is to practice pointers, pointer arithmetic, // pointers vs. arrays, and pointer parameters // // // Compile and run your program. When everything is fine, // print your .h and .cpp files and turn them to me or the TA. // ***************************************************************** #include < iostream > #include #include < string > #include < cstring > #include < cmath> #include #include #include //include vector class template //define a const array size const int SIZE = 35; //class size const int COLUMN = 11; //column size #include "lab2380_1_head.h" using namespace std; int main( ) { /************************************************************************ Part A: pointer declarations, operators: * : dereference operator & : address operator . : member selection operator ->: dereference member selection operator ************************************************************************/ //simple pointers int cat, dog; //two int vars int *intPtr; //an int pointer cat = 10; dog = 20; intPtr = &cat; //intPtr points to cat, i.e., intPtr has the address of cat cout << "cat is " <firstName <<" " << ptr->lastName <<" got " << ptr->grade << endl; ptr = &joe; //ptr points to joe //use dereference operator *, member selection operator ., and dereference member selection operator //to access members of a structure/class cout << (*ptr).firstName <<" " << (*ptr).lastName <<" got " << ptr->grade << endl; /************************************************************************ Part B: Pointer arithemtic, arrays vs. pointers ************************************************************************/ //var declaration int a[5]={10, 20, 30, 40, 50}; //an int array of size 5 studentType fun[3]; //a studentType array of size 3 int *myPtr; //an int pointer studentType *yourPtr; //a studentType point int i; //loop var //array name is a pointer pointing to the first element of the array //use array name pointer to access array elements cout<<"use array name ............ "< "; cin>> (yourPtr+i)->firstName; cout<<"Enter a first name => "; cin>> (*(yourPtr+i)).lastName; cout<<"Enter a grade => "; cin>> yourPtr[i].grade; } //cout students yourPtr = fun; for (i=0; i<3; i++) { cout << (yourPtr+i)->firstName <<" " << (*(yourPtr+i)).lastName <<" got " << yourPtr[i].grade <