Pointers in c and c++
Most programming like pointer in C and C++, because it gives the address of the memory which is being allocated. That is, it is the the value of the address. Through pointer you can access the memory directly which is being allocated. But if you do not use pointers properly there is a possibility that your program might crash. Therefore, the Java programming language do not use the pointers. That is, there is no way in java to access the memory allocation directly. This tutorial will show you how the pointers work internally. So, lets start
Introduction
Most new programmer are afraid to use the pointer in C and C++, because they are not sure if they miss something in their code and the pointer would effect the program. It aims to help beginners in C / C + + pointers to address with the least possible apprehension. (It is considered that you know the basics of the C and C++ programming language such as the variables, because to know the pointers internal working you have to know how the variable behaves in your code.
Pointer
A pointer is a variable, it is intended to contain a memory address, i.e a value identifying a location in memory. To distinguish a pointer to an ordinary variable, we precede the name of the sign '*' in his statement. Just have a look at the following example
The first line reserve a location for storing a memory address. The second line writes the address of x in this spot. Ok, We will try to understand this in more details.Code:int * px; px = &x;
When it is declare as int *px;, it means that
Here it reserves a storage place for the pointer px (box number 96 in the above diagram). At this stage there is no difference with this and an ordinary variable.
When it is assigned as px = &x;, it means that
It writes the address of x in the space reserved for the pointer px. Remember one pointer is used to store a memory address.
In the space reserved for the pointer px, we now have the address of x. This pointer has value as the address, so we can use this pointer to fetch (read / write) the value of x. For that we precede the name of the pointer with the de-reference operator '*'
So the following means,
Displays the value of x by pointer de-reference (10 in the case of the diagram).Code:printf ( "% d", * px);
We can similarly change the value of x
Now, value of x is 20Code:* px = 20;
Pointers must be initialized with an address valid, i.e that has been reserved in memory (allocated) by the program to be used. Imagine the previous statement, if we had not initiated the pointer with the address of x, the writing would be in an undisclosed location memory.
In the example above you have noticed that we gave a type the pointer (integer i.e int *), even if in a given system a pointer always has the same size (4 bytes for a system with 32-bit addressing). If you are unaware of the type of data the pointer will point to then you can give type void to pointer. It is customary to prefix the name of the pointer variable type the letter "p" this for better readability.
Bookmarks