Codesnipp.it Social Code Sharing

Thang Do

Everthing I know so far about pointer

by Thang Do on Oct 25, 2011

//****************************************************************************** //POINTER, DYNAMIC DATA, REFERENCE TYPE //****************************************************************************** //////////////////////////////////////////////////////////////////////////////// //POINTER //////////////////////////////////////////////////////////////////////////////// /*Declaration of pointer*/ int* pointer; /*Point to*/ int beta; pointer = &beta //& = address of /*Assign value*/ *pointer = 30 //This will make beta = 30 /*Pointing to a structure*/ struct People { int age; string name; } People Thang; //Declare and point PeoplePointer to Thang People* PeoplePointer = &Thang; /*Accessing data within structure*/ PeoplePointer->age = 20; //Thang.age = 20 (*PeoplePointer).age = 20; //Thang.age = 20 /*Declare an array of pointer*/ People* PeopleArr[20]; //access PeopleArr[2]->age = 20; //Assign age = 20 to the third People in the array /*Pointer Expression*/ int arr[100]; int* pointer; //The assignment statement pointer = arr; //has exactly the same effect as pointer = &arr[0]; //both store the base adress of arr into pointer /* *The expression pointer++ causes *pointer to point to the next element of the array */ pointer = &arr[0]; pointer++; //pointer = &arr[1]; //////////////////////////////////////////////////////////////////////////////// //DYNAMIC DATA //////////////////////////////////////////////////////////////////////////////// /*The use of new and delete*/ int* pointer; char* namePointer; pointer = new int; //Creats a variable of type int and store its address into pointer namePointer = new char[6]; //Creats a 6 element array and store its base address into namePointer delete pointer; //pointer now points at nothing. pointer is undefined delete [] namePointer; //namepointer is now undefined /*Prevent memory leak*/ int* ptr1 = new int; int* ptr2 = new int; *ptr2 = 44; //assign 44 to "value pointed by ptr2" *ptr1 = *ptr2; //*ptr = 44 delete ptr1; //delete ptr1 ptr1 = ptr2; //make ptr1 point to same address as ptr2 delete ptr2; //delete ptr2 ptr1 = NULL; //prevent dangling pointer cin.get(); return 0; //***************************************************************************************** //Example of DEEP COPY //COPY A CLASS OBJECT WITH ITS POINTERS //***************************************************************************************** class Message { public: //output operation void Print() const; //DEEP COPY OPERATION void CopyFrom( /* in */ Message otherMsg); //Constructor Message(/* in */ Time newTime, /* in */ const char* msgStr); //Copy-constructor Message(const Message& otherMsg); //Deconstructor ~Message(); private: Time time; char* msg; } //This is what happens in CopyFrom(Message otherMsg) //This function is to copy an existing class object //Already pointed to a dynamic data void CopyFrom(/* in */ Message otherMsg) { //Copy variable time = otherMsg.time; //Deallocate original delete [] msg; //Allocate new array msg = new char[strlen(otherMsg.msg) + 1] //Copy the chars strcpy(msg, otherMsg.msg); } //This is what happens in Message(const Message& otherMsg0 //This copy constructor will create a new class object Message(const Message& otherMsg) { //copy variable time = otherMsg.time; //Allocate new array msg = new char[strlen(otherMsg.msg) + 1]; //copy strcpy(msg, otherMsg.msg); } //*************************************************************************************** //////////////////////////////////////////////////////////////////////////////// //REFERENCE TYPE //////////////////////////////////////////////////////////////////////////////// /*Declaration*/ int& intRef; /*The use of reference type*/ int gamma = 26; int& intRef = gamma; intRef = 30; //gamme = 30

Can't see the comments? Please login first :)