Pointers Definition: A pointer is a data type recognized by C++ and C, but not by Java. A variable of data type pointer contains an address of some other data element stored in data memory (RAM), e.g., the address of the value stored in some other variable. The pointer variable itself is also stored at some address in data memory.
Diagram to learn programming pointers:
Pointers in different computer programming languages:
Pointers in C++/C: A pointer variable contains the address of other data elements. The values stored at the address can be changed, copied, etc., using only the address. Advantage of pointers in C/C++ is that programmers can directly access memory locations of the variables assigned while programming application programs either for embedded systems or operating system programs. Pointers should be used very carefully while programming your application in C/C++ since mirror error in pointer can terminate your whole application.
Pointers in Java: Java programmer does not have access to pointers. Pointers are used implicitly by the Java compiler/interpreter. Actually used more frequently by the Java compiler/interpreter than by the either the C++ or C compiler. Explicit manipulation of addresses/pointers by the Java programmer is forbidden.
Now lets simply understand how pointers are defined and used in C++ using simple example for better understanding pointers for practical purposes:
STEP 1: float A;
1. First of all, the data type of variable A is float.
2. The variable name A is for human/programmer use only. The compiler converts the variable name into an address/pointer that is used by the compiled code to access the value stored at that address.
3. The variable name is converted by the compiler into an address in data memory.
4. The address that is synonymous with to the variable name A contains the value that has been assigned to that variable.
5. Normally the code that manipulates the address that corresponds to the variable A is hidden from the programmer. 6. If desired by the programmer that address can be accessed, again it is only possible in C and C++ but not in JAVA. Variable address can be accessed using:
a. A reference to the variable A, as in &A.
b. A pointer to A (explained below).
STEP 2: float* PtrToA;
1. The notation float* defines the data type of pointer-to-float.
2. The variable name which follows is, then, the name of a variable which holds a value which is the address of some floating point value.
3. Alternate notation: float *PtrToA;
STEP 3: A = 3.1415;
1. Standard assignment statement.
2. The constant value 3.1415 is stored at the address in RAM that corresponds to the variable A.
STEP 4: PtrToA = &A;
1. A slightly different assignment statement.
2. The notation &A designates the address of the variable A.
3. Therefore: PtrToA = &A; causes the address of the variable A to be loaded into the address in RAM that corresponds to the pointer variable PtrToA.
In the end, lets program in C++ what we have explained above about pointers usage to see the actual results in programming how pointers are defined inside computer memory and how pointers are used inside the computer programming.
Samle example Code:
#include <iostream>
using namespace std;
void main()
{
float A; // declaration of a floating point variable
float* PtrToA; // declaration of a variable that will hold the// allocates four bytes of RAM and attaches // the memory to the program.
// address of a floating point number.
PtrToA = &A; // The value assigned to the pointer variable is the address of the floating point variable// in RAM that is synonymous with the variable name.
cout << endl << "The value of A is " << A << " while the address of A is " << PtrToA << endl << " and the value stored at the address " << PtrToA << " is " << *PtrToA << endl;
*PtrToA = 89.526;
cout << endl << "The value of A is " << A << " while the address of A is " << PtrToA << endl << " and the value stored at the address " << PtrToA << " is " << *PtrToA << endl;
}
The output of the above sample code to understand pointers would be:
0 comments:
Post a Comment