#What is this line of Turbo C (v1.0) supposed to do?

34 messages · Page 1 of 1 (latest)

empty spire
#

Hello,

I've been reading the C source code of the video game “Hard Drivin’” (1989) for the Atari ST, and I've stumbled upon this line of code, that doesn't compile under modern C compilers:

long* d;
int* p;

/* ... */

*d++ = *((long*)p)++;

I would like to decompose this line to make it much easier to understand. No need to write “clever” code.

Here what I've come up with, from what I understand the code is trying to do (since it doesn't compile):

long tmp = (long)*p;
++tmp;
*d = tmp;
++d;

If you need to have more context, the line in question in the full source code is visible here: https://github.com/malespiaut/hard-drivin-st/blob/master/src/display.c#L894

Is this correct?

Thank you all for your time!

GitHub

Leaked source code of Hard Drivin’ (1989) for the Atari ST, and experiments around it. - malespiaut/hard-drivin-st

uneven tangleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

nocturne spoke
#

What are p and d?

empty spire
#

What is this line of Turbo C (v1.0) supposed to do?

empty spire
nocturne spoke
#

My guess is it just reinterprets p as a long pointer, copies its value to where p points to, then increments both pointers by a long's size.

slow pilot
#

why not make p a char* and add sizeof(long) ?

long* d;
char* p;

/* ... */

*d++ = (long) *(p += sizeof(long));
nocturne spoke
#

Hard to know without at least seeing more context.

slow pilot
#

true true

#

or you could do the char thing even without altering the type of p:

#include <stdio.h>

int src_p[] = {1,2,3};
long src_d[] = {0,0,0};

long *d = src_d;
int *p = src_p;
char **c = (char**) &p;

int main()
{
    printf( "addr of p: %08x\n", (int) p);
    
    *(d++) = (long) *(*c+=sizeof(long));
    
    printf( "addr of p: %08x\n", (int) p);

    return 0;
}
empty spire
empty spire
slow pilot
empty spire
#

What does *(d++) means? @slow pilot
Does it mean

*d = /* ... */ ;
++d;

or does it mean

++d;
*d = /* ... */ ;
slow pilot
#

I think the first one

#

i just put the parenthesis for no reason ||bit of an addiction||

plain needle
#

Yes, it means the first one

empty spire
slow pilot
#

hm I see the problem. maybe try *(++d) = (long) *(*c+=sizeof(long)); as you are assigning the next value to the still not updated d in the above example

verbal wadi
#

or maybe valid in a compiler where operator precedence was different

#

my guess is that the intent is *d++ = (*((long*)p))++;

empty spire
empty spire
verbal wadi
#

precedence in standard C

#

precedence in non-standard turbo C

#

since in turbo C post-increment has the same precedence as dereferencing, the code is legal and parses as (*((long*)p))++

#

but in C suffix unary operators always come first, so it's parsed as *(((long*)p)++) and it doesn't make sense to increment an lvalue

verbal wadi
#

as you can imagine that was a bit of a nightmare

opal briar
#

but that seems like a reasonable possibility