# C++/Question

Question
A_O_A

Your,

Imran.

You can note my email address: sam_woods83@yahoo.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.

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:
int myvar;
myvar=57;
int *ptr;
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.

For understanding the last line, you need to understand what & means.
When I prefix & before the 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 would have given '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.
Sixth will be about data structures(stacks, queues etc).
That would be all about pointers.

Bi.
Samarth
Questioner's Rating
 Rating(1-10) Knowledgeability = 10 Clarity of Response = 10 Politeness = 10 Comment Thank you very much for helping me for completed my research. thank you very much

C++

Volunteer

#### Samarth Bartaria

##### Expertise

I can answer questions about pointers behaviour, their implementations and anamolous behaviour.Also, I speciallize in object oriented design and modelling,polymorphism in C++ and algorithm efficiency. Even questions related to database design,or simple basics about programming are welcome.

##### Experience

I have been using C++ for five years now for software development and scientific analyses.

Organizations
Currently, I am a student doing my computer engineering.