iDevGames Forums

Full Version: Pascal language (pointer) problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:

    t_LinkedListNodePtr = ^t_LinkedListNode;
    t_LinkedListNode = record

procedure p_ReverseLinkedList( var p_first:t_LinkedListNodePtr );

    procedure p_ReverseLinkedListNodes( p_previous:t_LinkedListNodePtr; var p_current:t_LinkedListNodePtr );
        if p_current <> nil then
            p_ReverseLinkedListNodes( p_current, p_current^.m_next );
        if p_current = nil then
            p_first := p_previous
            p_current^.m_next := p_previous

    p_ReverseLinkedListNodes( nil, p_first )

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:

typedef struct t_LinkedListNode_struct
    struct t_LinkedListNode_struct *m_next;
    char m_char;

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;
        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.
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.
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.
Huzzah, thanks to jareds of the xkcd forums I know now where I was going wrong Grin
Reference URL's