Pascal language (pointer) problem

Moderator
Posts: 691
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
Quote this message in a reply
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.
Quote this message in a reply
Moderator
Posts: 691
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
Quote this message in a reply
Moderator
Posts: 691
Joined: 2002.04
Post: #4
Huzzah, thanks to jareds of the xkcd forums I know now where I was going wrong Grin

Mark Bishop
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  another c pointer question NelsonMandella 3 3,725 Mar 26, 2010 03:49 AM
Last Post: DoG
  Problem linking Squirrel language with SqPlus bronxbomber92 8 5,415 Aug 3, 2007 05:46 PM
Last Post: bronxbomber92
  Diferences between array and pointer initialized strings in C ferum 16 6,499 Jan 4, 2006 11:38 PM
Last Post: kelvin
  Member function pointer LongJumper 6 4,694 Oct 31, 2005 05:53 PM
Last Post: LongJumper
  Cocoa Method to C Pointer KiroNeem 11 6,399 Sep 12, 2005 10:08 AM
Last Post: Zekaric