Sir I wanted to expert in using Pointers in C++. Please help me I wanted to learn from you about pointer from A to Z. So when I use pointer I know all about pointer. So please help me for learning about pointer.
Imran Ahmad Mughal
You can note my email address: email@example.com
When I go on vacation next time you can contact me there.
I would be covering pointers in a series of mails to you. If you could give me your email id, it would be easier for me because then I can send you figures and diagrams also.I can not do that on this page.
Lets start with the basics,which you may already know.
The primary memory of a computer is known as RAM.Every thing the CPU needs has to be loaded into ram.The problem with ram is that it is temporary. That is when you switch off your computer, it is deleted. For permanent storage, we use harddisks and floppy drives.
For example, you can store an image on your hard drive. Every time you access it, the image will be firstloaded into RAM,and then it will be opened.
RAM in divided into separate areas(or blocks).Each block can store certain amount of data(say 1 byte). Each block has a certain address.We refer to this address in hexadecimal notation.For convenience though, in these chapters we will consider them as decimals.That is, we will call them by numbers(1,2,3 etc)
Whenever in any programming language(I will consider c++), you declare a variable, like "int myvar;", space has to be allocated for the variable. Different kind of variables use different amount of space.For example, int uses 2 bytes, char uses 1 byte and so on. So for my variables "myvar", which is int, 2 blocks of 1 byte each would be reserved in the RAM. Let us assume that blocks 12 and 13 are reserved. Although 2 blocks are reserved,I will say that the address of "myvar" is 12. Only the address of first block is considered. Now if I say "myvar=57", the number 57 would be stored in blocks 12 and 13.
It is not stored as 5 and 7. The number is stored as binary.
A pointer holds the address of a memory block. pointers also have types. For example, an int pointer can only hold the address of a block which is storing int variable. Since pointers are also variables, they also occupy memory blocks(1 byte). Thus in effect you have a memory block which is holding the address of another memory block.
I give an example:
ptr = & myvar;
You know now what the first 2 lines are doing.
int *ptr is the way you declare a pointer. A * tells the compiler that this is a pointer and not an ordinary variable.This is the first use of *.Its other use we will see in my next mail.
For understanding the last line, you need to understand what & means.
When I prefix & before a variable,I mean the address of that variable.
For example, variable "myvar" is stored at location 12 and 13.
So &myvar would give me 12.
When I say 'ptr=&myvar', I mean ptr is equal to the address of myvar, or ptr =12.
Now suppose instead of giving 'ptr=&myvar', I give 'ptr=myvar', pointer ptr would not have stored the address of myvar, but myvar itself. That is, ptr=57, which we dont want.
Hence, we have to say 'ptr=&myvar'. & is important here.
Here I end my first mail. In next mail I will write about accesing the variable pointed by a pointer, pointer to a pointer and arrays.
In third mail I will send you sample programs.
In fourth we will talk about user defined data types(classes and objects) and pointers to them.
Fifth would be about polyporphism.
Sixth will be about data structures(stacks, queues etc).
That would be all about pointers.
If you send me your email address, I will send diagrams along with the notes.