Recursive
2012-06-25 00:58:37
Need help understanding pointers. Someone* explain please what they do and how they can be used in a program.
2012-06-25 00:58:37
2012-06-25 01:48:18
void ChangeValueOfX(int &SomeValue)
{
SomeValue = 10; //We have the address of X so lets change the value at that address from 5 to 10.
}
int main()
{
int X = 5;
ChangeValueOfX(X); //X was passed by reference.
}
char Buffer[5] = {'a', 'b', 'c', 'd', 'e'};
char *P = B; //P will point you to the address in memory where 'a' is located. Aka Buffer[0].
int A[5] = {1, 2, 3, 4};
int *P = &A; //We declare a pointer called P and make it point to the address of A[0];
cout<< *P <<endl; //This will print A[0] aka 1. This is because we derefenced our Pointer inorder to get the value of A[0].
//We can also change the value of A[0] using the same method.
*P = 9000; //Now A[0] will have a value of 9000. We dereferenced out pointer to access the value of Cell A[0] and change it indirectly.
int SizeOfFile(File* pFileHandle)
{
//Some code to determine file size..
return FileSize;
}
int main()
{
char Buffer[100]; //Create a char array buffer of a static size of 100. We know the size here.
fread(&Buffer, 1, 100, PointerToAFileHandleHere); //Our Buffer now gets filled with the data from some file that we read. Note that we had to pass Buffer by reference. AKA it's address to fread so that it can fill the buffer.
//Lets suppose that we do not know the size of this file. How do we know how large to make the buffer?
char* pBuffer = new char[SizeOfFile]; //DYNAMICALLY allocate a buffer and return the pointer to it!
//In the above, pBuffer is a pointer to the memory location where we allocated a large enough amount to hold our file.
//new char[SizeOfFile] allocated a buffer and assigned it's pointer to pBuffer.
//The only way we can now fill the memory with data from a file is through that pointer aka. pBuffer.
int FileSize = SizeOfFile(pFileHandle);
fread(pBuffer, 1, FileSize, pFileHandle); //Note that there is no address operator because pBuffer is already a pointer and it pointers to the address where it allocated memory. Thus the memory at that address will get filled with file contents which we can access through pBuffer.
//Now pBuffer holds the data from the file.. We do whatever we want with it such as printing it.
std::cout<< pBuffer <<std::endl;
//Lets save it to a string.
std::string DataHolder(pBuffer, FileSize);
//Now we have to CLEAN UP our pointers by using Delete.
delete[] pBuffer; //Now pBuffer Pointers to nothing. We deleted the memory buffer than it pointed to!
}
typedef union RGB
{
int Color;
struct
{
unsigned char Blue, Green, Red, Alpha;
} Inner;
} PRGB; //PRGB is an object (pointer in some cases).
int main()
{
PRGB A = new RGB[10]; //this is the essentially same as RGB* A. Like I said before, it's a pointer.
RGB B; //This is just a normal class object. It is not a pointer.
std::cout<< A[0]->Inner.Blue; //We cannot do A[0].Inner.Blue because A is a pointer to an array of the RGB struct and we must go through A and get the data it points to.
std::cout<< B.Blue; //We can definitely do this. Blue is a member of object B.
delete[] A; //Pointers to dynamic memory must be cleaned! Thus the memory that is pointed to by A is deleted with this command.
//See the difference? A Pointers to an RGB structure and inorder to get the value of Blue, we have to find out what A Points to. Inother words, B is "pointed to by" A.
RGB* M = new RGB; //Dynamic allocation of an object M. Note that M is just a pointer to an RGB struct. It is not a pointer to an array of RGB struct. Also note that I did not use PRGB M. This is because PRGB is a pointer to an Object of type RGB. RGB* M = new RGB declares that M is now a pointer to an object of type RGB. It does not make sense to do PRGB M = new RGB; Without the index operator []. M is a single object, A is an array of objects.
cout<<M->Inner.Blue; //Inner.Blue is pointed to by M. Thus -> Must be used to access them. We still have to go through M to get to Blue.
delete M; //Delete dynamically allocated memory which is pointed to by M. Again note that we do not have [] because it is not an array.
}
#include <iostream>
using namespace std;
int main()
{
int B[5] = {9, 6, 7, 4, 11}; //Declare B as an array of Integers;
int *P = B; //P Points to the location of B[0]. Yes, that's correct. P Points to the address of the very first value of the array.
cout<< P <<endl; //Will print some giberish hexedecimal stuff. This represents the address of B[0]. Like I said before.
cout<< *P <<endl; //We derefence P. So this means *P is the same as B[0]. Thus *P = 9;
cout<< ++*P <<endl; //We incremeted the value of *P. Since *P = B[0] which has a value of 9. ++*P = 10;
P++; //Make P Point to the next Location in our Array. AKA the address of B[1] in memory.
cout<< *P <<endl; //*P is now equal to 6. Since cell B[1] has a value of 6.
*P = 99; //Change the value of Cell #2 to 99.
cout<< B[1]; //This is cell #2. It has a value of 99 since we indirectly changed it.
}
2012-06-26 15:47:36