## Pascal language (pointer) problem

Moderator
Posts: 706
Joined: 2002.04
Post: #1
Greetings, as part of a (sub-degree level) course I'm studying I'm revising Pascal (having last touched the language in, I think, 2001...), as the examinations which are part of the course often present questions – or expect questions to be answered – in a Pascal-esque language. I'd been working through the programming exercises in TURBO Pascal : An Introduction to the Art and Science of Programming (Second Edition) by Walter J. Savitch without issue, but I've now encountered a programming exercise which has revealed a gap in my understanding of pointers in Pascal:

On page 688 Walter J. Savitch Wrote:12. Write a procedure that takes a (singly) linked list and reverses the order of the nodes in the linked list. For concreteness, make it a linked list of characters.

So I threw together a solution to the programming exercise:

Code:
```type     t_LinkedListNodePtr = ^t_LinkedListNode;     t_LinkedListNode = record         m_next:t_LinkedListNodePtr;         m_char:char     end; procedure p_ReverseLinkedList( var p_first:t_LinkedListNodePtr );     procedure p_ReverseLinkedListNodes( p_previous:t_LinkedListNodePtr; var p_current:t_LinkedListNodePtr );     begin         if p_current <> nil then             p_ReverseLinkedListNodes( p_current, p_current^.m_next );         if p_current = nil then             p_first := p_previous         else             p_current^.m_next := p_previous     end; begin     p_ReverseLinkedListNodes( nil, p_first ) end;```

But when the procedure p_ReverseLinkedList is passed a linked list containing the sequence "Mark", the linked list becomes just "k". After a frustrating time trying (and failing) to identify the error in my logic, I threw together a C (rough) equivalent of my solution to the programming exercise to test my logic:

Code:
```typedef struct t_LinkedListNode_struct {     struct t_LinkedListNode_struct *m_next;     char m_char; } t_LinkedListNode; static t_LinkedListNode *g_first; void p_ReverseLinkedListNodes( t_LinkedListNode *p_previous, t_LinkedListNode *p_current ) {     if( p_current != NULL )         p_ReverseLinkedListNodes( p_current, p_current->m_next );     if( p_current == NULL )         g_first = p_previous;     else         p_current->m_next = p_previous; } void p_ReverseLinkedList( void ) {     p_ReverseLinkedListNodes( NULL, g_first ); }```

The function p_ReverseLinkedList works without error – when passed a linked list containing the sequence "Mark", the linked list becomes "kraM" – so it looks like my logic is correct.

And... I don't understand why my original Pascal solution to the programming exercise fails to function as expected; it looks like whilst p_current is treated as a variable parameter (correct), the m_next attribute of p_current is treated as a value parameter (incorrect); could somebody more fluent in Pascal please suggest where I'm going wrong...?

Nb. I'm using CodeWarrior.

Mark Bishop
--
Student and freelance OS X & iOS developer
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
Hmm. I can't seem to find where the user list went to in the redesign.

You should search out Ingemar. He's a bit of a Pascal wiz. Still uses Free Pascal last I heard.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Moderator
Posts: 706
Joined: 2002.04
Post: #3
Just to add a tiny elaboration: I'm not sure if it is relevant, but the project settings have two deviations from the default ANS Console 68k project settings: ANS Conformance is turned off and the IO Model is set to Turbo Pascal.

Mark Bishop
--
Student and freelance OS X & iOS developer
Moderator
Posts: 706
Joined: 2002.04
Post: #4
Huzzah, thanks to jareds of the xkcd forums I know now where I was going wrong

Mark Bishop
--
Student and freelance OS X & iOS developer