Silverfrost Forums

Welcome to our forums

pointers & associated/disassociate attributes

3 Jan 2022 4:26 #28641

I am at the beginner level with pointers & targets and linked lists. I am working my way through Dr. Tanja van Mourik’s 2005 Fortran 90/95 tutorial. But I also use Silverfrost’s Fortran 95 tutorial however it is less useful because it somewhat assumes one already knows a lot of the material. With this preamble, I am trying to confirm here that I correctly understand how the first link is established in a linked list.

nullify (first) nullify (current) ! read in a number, until 0 is entered do read*, number if (number == 0) then exit end if allocate (current) ! create new link current%i = number current%next ⇒ first ! point to previous link first ⇒ current ! update head pointer end do

The do loop is used to create the links. Before the first loop, pointers first & current are nullified. Thus, they are both disassociated. The first loop is entered and reads in an INTEGER to number. Checks to see if number=0 to exit. If not, pointer current is allocated. Hence current is now associated with memory. The data item current%i is next equated to number.

On the next line, the pointer next is pointed to pointer first in the statement current%nextfirst. first at this time is disassociated. It appears this also results in current%next being dissociated?

On the next line, pointer first is pointed to pointer current. Thus, pointer first is now associated. However, pointer current%next does NOT update to associated?

Here is my issue and question. In all the prior tutorial discussions of pointers & targets and variables once the pointer – target relationship is established, a change of one, changes the other. They update. Does this only apply to variables? In the case of pointers and the attribute associated/disassociated, and a same pointer – target relationship, the associated/disassociated attribute does NOT update? I have not found any discussion of this nuance in a tutorial thus far.

If this occurs then the pointer of this link remains disassociated and thus identifies it as the last link of a linked list.

3 Jan 2022 8:30 (Edited: 8 Jan 2022 1:06) #28642

The code that you showed produces a backward linked list. If you compile and run the complete program from p.59 of the lecture notes that you referred to, you will see the numbers printed out in reverse order.

As you have probably learned by now, one can also construct forward linked lists and doubly linked lists, and the choice of list type is guided by what one plans to use the list for.

In Fortran, 'pointer' and 'target' are attributes of variables; we do not have detached pointers (as in C) which contain addresses of objects of any type. In further contrast with C pointers, there is no de-referencing operator such as the '*' of C. These aspects are described in the lecture notes, as well.

If you add the following statement before the first END DO in the program, your questions will probably be answered.

print *,current%i,associated(current%next)

By the way, if you wish to post code with the formatting retained, paste in the code, choose the code block with the mouse, and click on the 'code' button in the menu.

8 Jan 2022 12:40 #28647

Thanks for your reply.

Please login to reply.