Pointers

=Pointers=

A pointer is a special data type whose value refers directly to another value stored elsewhere in the computer memory using its address. Pointers are important in forming large data structures such as linked lists, stacks, and binary tree.

In C++
Initializing a data type as a pointer in C++ is as simple as adding an asterisk(*) in front of the variable name. The address of a data type can be accessed by the ampersand(&) symbol followed by the variable name. To access the value the pointer is pointing to use the asterisk(*) followed by the value name. Pointers in C++ also have the ability to iterate through memory by incrementing(++) the address location. This is useful for scanning through arrays. code format="cpp" main{ int myValue = 5; int *ptr; //declared the pointer ptr = &myValue; //set the point to the values address printf("%d\n", *ptr); //5

myValue = 7; printf("%d\n", *ptr); //7

int myArray[] = {1,2,3,4,5, 0}; int *arrayPointer = myArray; while(arrayPointer){ //this will loop until 0 printf("%d ", arrayPointer); arrayPointer++; //increment the pointer in memory }//output: 1 2 3 4 5 } code

In Java
Pointers in java are everywhere. Every time you declare a class-object you are making a pointer. Since java is a high level language much of its operations are done in the heap or dynamic memory, so everything but the most basic data types are pointers. code format="java" public class PtrDemo{ public class MyClass{

}   public static void main(String[] args){ MyClass classObject = new MyClass; //this is a java pointer

int x = 5; //this is not a pointer } } code