# POINTER (What is it ) ? Classic List Threaded 8 messages Open this post in threaded view
|

## POINTER (What is it ) ?

 can someone explain what is pointer that is being talked about in the lecture videos ? Shimon talks about pointer as being an address but to what ? can someone explain in terms of hack assembly language and a sample code for "pointer" in hack assembly language ?
Open this post in threaded view
|

## Re: POINTER (What is it ) ?

 Pointer is a variable or a data element, which holds the address of some data. For example the code snippet ```var Array a, b; let a = Array.new(10); let b = a; ``` creates a new data structure and assigns its address to the variable a. That is, a points to this array. The next line assigns to b the value of a, which is the address of the array. So b also points to the same array. In terms of HACK assembly, doing something like: ```@x // Stores in register A the address of x D=A @0 M=D // Stores in R0 the address of x ``` Now R0 can be thought of as a pointer to x. Pointers are sometimes called references. They are used in a variate of situations. For example, consider the linked list. Linked list is a structure, where each node holds an element and points to the next node. Here is how it looks conceptually: ```+---+ +---+ +---+ | 1 | | 2 | | 3 | | o-+---> | o-+---> | o-+---> X +---+ +---+ +---+ ``` To implement this, we'll make each node hold two pieces of information: the data, which is a number in this case, and a pointer to the next node. The last node will hold have a special value in the pointer, to indicate that it's last. Normally this value is 0 and it's called a NULL pointer. Let's say that we put the first node in address 100: ```100: 1 101: 274 ... 141: 3 142: 0 ... 274: 2 275: 141 ``` So, the first node contains the value 1 and points to the second node at address 274. The second node contains the value 2 and points to the third node at address 141. The third node contains the value 3 and is the last node in this list, because it points to address 0. With pointers you can create much more complex data structures. For example, consider what will happen if you put 100 in address 142.
Open this post in threaded view
|

## Re: POINTER (What is it ) ?

 so basically it is just a value in a register that is interpreted as address of some other register .? ......BELOW is the code you posted.......... @x      // Stores in register A the address of x D=A @0 M=D   now register 0 has value x in it .So how can i get the data stored in Register x using register 0  . This is what pointer application is right .? Thank you for answering ivant.
Open this post in threaded view
|

## Re: POINTER (What is it ) ?

 > so basically it is just a value in a register that is interpreted as address of some other register .? Bingo
Open this post in threaded view
|

## Re: POINTER (What is it ) ?

 won't that be inefficient compared to accessing it directly ? and how to interpret that stored value as address ?
Open this post in threaded view
|

## Re: POINTER (What is it ) ?

 In reply to this post by Minoshi Minoshi wrote so basically it is just a value in a register that is interpreted as address of some other register .? Or in a memory register. Minoshi wrote @x // Stores in register A the address of x D=A @0 M=D now register 0 has value x in it .So how can i get the data stored in Register x using register 0 . This is what pointer application is right .? You can get the value through the M pseudo-register: ```D=M ```
 thanks for answering . cleared my doubts . 