forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

pointers & associated/disassociate attributes

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
Michael_Connelly



Joined: 13 Jul 2017
Posts: 10
Location: Sandusky, OH

PostPosted: Mon Jan 03, 2022 5:26 am    Post subject: pointers & associated/disassociate attributes Reply with quote

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%next => first. 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.
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1884

PostPosted: Mon Jan 03, 2022 9:30 am    Post subject: Reply with quote

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.

Code:
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.


Last edited by mecej4 on Sat Jan 08, 2022 2:06 am; edited 1 time in total
Back to top
View user's profile Send private message
Michael_Connelly



Joined: 13 Jul 2017
Posts: 10
Location: Sandusky, OH

PostPosted: Sat Jan 08, 2022 1:40 am    Post subject: pointers & associated/disassociate attributes Reply with quote

Thanks for your reply.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group